问题描述
这里有控制器代码:
$id = 2;
$user = User::where('userToken','=',$token)
->with('userPost($id)')
->first();
这里有模型代码:
public function userPost($id) {
return $this->hasMany(PostPhotos::class,'user_id','id')
->with(['getlikes' => function($q) use ($id) {
$q->where('likeStatus','like');
$q->where('userclick_id',$id);
}]);
}
你能帮我解决这个问题吗?
解决方法
你的逻辑是错误的。因为 with
函数用于显示关系。在模型中,您必须仅将关系显示为 belongsTo
或 oneToMany
。但是您可以使用示波器实现您的目标。
只是你需要从这里研究:
https://laravel.com/docs/8.x/eloquent#query-scopes