execute multiple quires at once in laravel

2019-09-06 21:10发布

问题:

I need to execute 3 quires when user comes to main page.

 public function index()
{
    $slider = \App\Slider::select("title", "poster", "link", "sub_title")->translate()->orderBy("created_at", "asc")->get();
    $services = \App\Page::getPage(24)->tabs()->translate()->get();
    $partners = \App\Partner::select('id', 'title', 'link')->translate()->get();

    return view('Front/index', compact('slider', 'services', 'partners'));
}

as you can see, I need to get images from slider, take page data and take some company partners info. so i execute 3 quires to get what i want. is there way to make only one query and union all these 3 quires in one? I want something like multi_query function in php. no matter it would be in eloquent or query builder. p.s. I don't eloquent relationships, these data aren't related to each other

回答1:

just put ur query inside $qry=DB::select("your_query");

 return view('your_view',compact('qry'));

you can see following also for better unterstand

$emp_id="5623";
$var_start_date=$request->startdate;


$data_query= DB::select("SELECT  orinfo.*,
                                 chinfo.name as chname

            FROM order_info orinfo, ch_info chinfo
            WHERE orinfo.ch_id= chinfo.ch_id 
            AND orinfo.emp_id= ? 
            AND
            to_char(orinfo.order_Date,'mm/dd/yyyy') BETWEEN ? AND ? order by orinfo.order_Date desc",[$emp_id,$var_start_date,$var_end_date]);