Home > Enterprise >  Laravel Call to a member function paginate() on array if join 3 queries from database
Laravel Call to a member function paginate() on array if join 3 queries from database

Time:07-21

Found a solution how to connect 3 queries from the database. But I can't try pagination on information My code:

$ads_default = AdModel::where('status','=', 1)->where('category_id','=',$this_category_id)->where('moderation','=', 0)->where('ad_diamond','=', 0)->where('ad_vip','=', 0)->orderBY('ad_pos', 'asc')->get();
    $ads_vip = AdModel::where('status','=', 1)->where('category_id','=',$this_category_id)->where('moderation','=', 0)->where('ad_vip','=', 1)->orderBY('ad_vip_pos', 'asc')->get();
    $ads_diamond = AdModel::where('status','=', 1)->where('category_id','=',$this_category_id)->where('moderation','=', 0)->where('ad_diamond','=', 1)->orderBY('ad_diamond_pos', 'asc')->get();
    $merge_d_v = $ads_diamond->merge($ads_vip);
    $merge_d_v_r = $merge_d_v->merge($ads_default);
    $data['all_union_ads'] = $merge_d_v_r->all()->paginate(20);

CodePudding user response:

You can't call paginate on collections, after you call get() on a query, it will resolve the query. This can simply be some orWhere() logic.

Note the logical grouping with the closures, to avoid it being category_id = 1 AND ad_vip = 1, where instead it will be similar to category_id = 1 AND (one_condition OR another condition).

AdModel::where('status', 1)
    ->where('category_id', $this_category_id)
    ->where('moderation','=', 0)
    ->where(function ($query) {
        $query->orWhere(function ($query) {
            $query->where('ad_diamond','=', 0)
                ->where('ad_vip','=', 0)->orderBY('ad_pos', 'asc');
        })->orWhere('ad_vip','=', 1)
        ->orWhere('ad_diamond','=', 1);
    })->paginate();
  • Related