laravel whereNotIn where子查詢

子查詢寫法laravel

$stores = Stores::select('id','name')->whereNotIn('id', function ($query) use($id){
            $query->select('stores_id')->from('fook_article_stores')->where('fook_article_stores.article_id', $id);
        })->get();

 

Stores::where('verification_pass',$verification_pass)->where('id','<>',$id)->where('business_id', function ($query) use($id){
                $query->select('business_id')->from('fook_stores')->where('fook_stores.id', $id);
            })->get();

 

laravel WhereNotIn 高效寫法

  • Relation 表結構spa

    • id
    • userId
    • toUserID
    • relation
  • 要求:篩選出沒有與本用戶創建關係的用戶code

  • 高效寫法blog

$users = User::whereNotIn('id', function ($query) use($user){
            $query->select('toUserId')->from('relation')->where('relation.userId', $user->id);
});
  • 低效率寫法
$actionRelationsArray = Relation::select('toUserId')->where('userId', $user->id)->get()->pluck('toUserId');
$users = User::whereNotIn('id', $actionRelationsArray);
相關文章
相關標籤/搜索