-4

我想知道如何在with函数中传递变量。代码如下:

这里我们有控制器代码:

$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);
        }]);
}

你能帮我吗?

4

1 回答 1

0

你的逻辑是错误的。因为with函数用于显示关系。在模型中,您必须仅将关系显示为belongsTooneToMany。但是您可以使用示波器来实现您的目标。只是你需要从这里研究: https ://laravel.com/docs/8.x/eloquent#query-scopes

于 2021-07-19T07:31:51.813 回答