如何通过控制器访问 Laravel 中模型的值?

How to access the value of a Model in Laravel through Controller?

我正在尝试获取 table 的 'id',其中 'user_id' == auth()->id() (检查属于用户的行)。

这是我到目前为止所取得的成就:

有什么可以克服的,所以我只能在 return 中获得 'id' 值?

get() 将 return 编辑一个集合。您可以改用 first() 然后您可以直接访问模型。之后您可以追加 ->id。它只会 return 客户端 ID。

'client_id' => Clients::select('id')->where('user_id', auth()->id())->first()->id;

// output the id as int