Before I describe the problem here for a long time, short and sweet: Laravel 5.4
In theory files should be returned with the corresponding name and ID of the user.
return Files::with(['user' => function($query) {
$query->select('id', 'name');
}])->select('filename', 'uid')->paginate();
However, the ID is missing in the query.
select `filename`, `uid` from `files` limit 15 offset 0
select `id`, `name` from `users` where `users`.`id` in ('')
The relations in the models are available
//User.php
public function files()
{
return $this->hasMany('App\Files');
}
//Files.php
public function user()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
Does anyone have any idea why? After all, there is no way to pass the ID as a parameter.
Eager loading can only work when the parent query selects the foreign key column:
return Files::with(['user' => function($query) {
$query->select('id', 'name');
}])->select('filename', 'uid', 'user_id')->paginate();
^^^^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.