简体   繁体   中英

How can I show and pagination two queries in the laravel blade?

I want to show $personnals and $users in a table in the blade. I know, I have to use @foreach . But I can't. Because, I have two queries and I can't pagination and show those.

$personnals = DB::table('orders')
            ->where('pay_type', 1)
            ->leftJoin('personnels', 'orders.personnel_id', '=', 'personnels.id')
            ->leftJoin('users', 'personnels.user_id', '=', 'users.id')
            ->select(['name AS personnelName', 'lastname AS personnelLastName'])
            ->get();

 $users = DB::table('orders')
        ->where('pay_type', 1)
        ->leftJoin('users', 'orders.user_id', '=', 'users.id')
        ->select(['name AS userName', 'lastname AS userLastName', 'orderCode'])
        ->get();

$personnals has 100 elements and $users has 100 elements ,too. Every elements is an array. I want to merge these arrays together and have one array with 100 array elements.

For example: I want something like $result:

$personnals = [ [ 'a'=>'blue', 'b'=>'red' ], [  'a'=>'green', 'b'=>'black' ] ];
$users = [ [ 'c'=>'yellow', 'd'=>'orange' ], [  'c'=>'white', 'd'=>'pink' ] ];

$result = [
            [ 'a'=>'blue', 'b'=>'red', 'c'=>'yellow', 'd'=>'orange' ] ,
            [  'a'=>'green', 'b'=>'black', 'c'=>'white', 'd'=>'pink' ]
        ];

How can I show these in blade with pagination?

OR

Can I show all in one query?How?

Thanks.

you can use union

$personnals = DB::table('orders')
            ->where('pay_type', 1)
            ->leftJoin('personnels', 'orders.personnel_id', '=', 'personnels.id')
            ->leftJoin('users', 'personnels.user_id', '=', 'users.id')
            ->select(['name', 'lastname', 'orderCode']);


 $result = DB::table('orders')
        ->where('pay_type', 1)
        ->leftJoin('users', 'orders.user_id', '=', 'users.id')
        ->select(['name', 'lastname', 'orderCode'])
        ->union($personnals)
        ->get();

try above!


As you want only 100 result instead of 200 than so you are expecting join instead of union

 $result = DB::table('orders')
            ->where('pay_type', 1)
            ->leftJoin('personnels', 'orders.personnel_id', '=', 'personnels.id')
            ->leftJoin('users AS u1', 'personnels.user_id', '=', 'u1.id')
            ->leftJoin('users AS u2', 'orders.user_id', '=', 'u2.id')
           ->select(['u2.name AS userName', 'u2.lastname AS userLastName', 'orderCode', 'u1.name AS personnelName', 'u1.lastname AS personnelLastName'])
            ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM