提问人:XtyLish Sam 提问时间:10/12/2022 更新时间:10/12/2022 访问量:598
Laravel Closure Function is not working (with) 关系
Laravel Closure Function is not working (with) relation
问:
开发人员您好,我希望你们都很好。 我已经有 2 或 3 天的问题了,我不知道如何解决这个问题。代码如下。但它总是回来
错误:方法名称必须是第 764 行文件 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php 中的字符串
$feeds = Feed::where("id",$request->feed_id)
->with("media","likes","customer",['comments'=>function($query){
foreach ($query as $comment){
return $comment->id;
}
}])
->first();
答:
0赞
JS TECH
10/12/2022
#1
您可以使用嵌套来获取用户名和图片。with()
$feed = Feed::where("id",$request->feed_id)
->with([
"media",
"likes",
"customer",
"comments"=> fn($query) => $query->with('user')
])
->first();
foreach ($feed->comments as $comment) {
$user_name = $comment->user->name;
$user_picture = $comment->user->picture;
}
评论
comments