我将传递变量问题传达给我的雄辩模型。我会举例说明 数据库:
书:
-id
-name
用户:
-id
-name
愿望清单:
-book_id
-user_id
现在我得到了书的记录。如果用户将这本书放在愿望清单中,我会嗤之以鼻。
我有这样的关系:
预订雄辩的模型:
public function wishList($userId)
{
return $this->hasOne('App\Model\Pivot\WishList', 'comics_record_id')->where('user_id',$userId);
}
但是当我运行此代码时,请给我一点意见。
图书管理员:
$record = Book:find(1);
$record->wishList(1);
我也尝试在雄辩的模型中制作二传手。 没有工作:(
请求帮助,对不起我的英语。
答案 0 :(得分:0)
首先,你传递$ userId并在where子句中使用$ userID .... undefined variable error here。
答案 1 :(得分:0)
首先改变你与此的关系 -
public function wishList()
{
return $this->hasMany('App\Model\Pivot\WishList', 'book_id', 'id');
}
然后你的关系将是 -
$book = Book::with('wishList')->where('id',$book_id)->whereHas('wishList', function($q) use ($user_id){
$q->where('user_id',$user_id);
})->first();
if($book){
//show true here
}
else{
//show false here
}