Where NOT in pivot table

前端 未结 7 1757
栀梦
栀梦 2020-12-13 19:02

In Laravel we can setup relationships like so:

class User {
    public function items()
    {
        return $this->belongsToMany(\'Item\');
    }
}
         


        
7条回答
  •  忘掉有多难
    2020-12-13 19:20

    For simplicity and symmetry you could create a new method in the User model:

    // User model
    public function availableItems()
    {
        $ids = \DB::table('item_user')->where('user_id', '=', $this->id)->lists('user_id');
        return \Item::whereNotIn('id', $ids)->get();
    }
    

    To use call:

    Auth::user()->availableItems();
    

提交回复
热议问题