In Laravel you can fetch a related database entry in one query with the with method like this:
$customer = Customer::with('user')->where([
'id' =>'1'
])->first();
Which will return a customer and it's related user object. Is it possible to apply a filter to this with item? I thought that this would be possible:
$customer = Customer::with('user')->where([
'id' =>'1',
'users.status' => 'activated'
])->first();
But that does not seem to work, it tries to find a column "users.status" instead of a column status in the users table.
You can use Eager loading constraints. It allows you to perform queries on related models.
Like this:
$customer = Customer::with(['user' => function($query){
$query->where('status', 'activated'); //in the user table
}])->where('id', '1')->first(); //in the customer table
You can read the eager load constaints part for more informations:
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加