Ad
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.
Ad
Answer
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();
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad