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

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();

Related

Laravel query count total rows

I have this query:
$counts = DB::table('projects')
->join('prject_links', 'prject_links.project_id', '=', 'projects.id')
->join('links', 'links.id', '=', 'prject_links.link_id')
->join('segments', 'segments.id', '=', 'links.segment_id')
->join('hthree_regions', 'hthree_regions.id', '=', 'segments.hthree_id')
->join('areas', 'areas.id', '=', 'hthree_regions.area_id')
->join('zone_regions', 'zone_regions.id', '=', 'areas.zone_id')
->select(
'zone_regions.id as regions',
'areas.id as provinces',
'hthree_regions.id as cities',
'segments.id as segments'
)
->get();
what I'm looking to have as result is something like:
counts => [
regions => 20,
provinces => 10,
cities => 7,
segments => 5
];
What do i get currently is confusing result like:
any idea about how to fix my query?
Use groupBy and count(distinct field) to get the field's count in each project:
->selectRaw(
"CONCAT('project ', projects.id) as project,
COUNT(DISTINCT zone_regions.id) as regions,
COUNT(DISTINCT areas.id) as provinces,
COUNT(DISTINCT hthree_regions.id) as cities,
COUNT(DISTINCT segments.id) as segments"
)
->groupBy('projects.id')
->get();
This might be your solution. Run it and let me know if that helps
$counts = DB::table('projects')
->join('prject_links', 'prject_links.project_id', '=', 'projects.id')
->join('links', 'links.id', '=', 'prject_links.link_id')
->join('segments', 'segments.id', '=', 'links.segment_id')
->join('hthree_regions', 'hthree_regions.id', '=', 'segments.hthree_id')
->join('areas', 'areas.id', '=', 'hthree_regions.area_id')
->join('zone_regions', 'zone_regions.id', '=', 'areas.zone_id')
->groupBy('projects.id')
->selectRaw(
'count(zone_regions.id) as regions',
'count(areas.id) as provinces',
'count(hthree_regions.id) as cities',
'count(segments.id) as segments'
)
->get();

Reducing database query sizes in laravel

I'm looking to reduce the my query size in laravel.
My query looks something like this (I shortened it, it's about 10 times this amount of lines):
$users = User::where("interface_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("interface_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("web_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("web_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("illustration_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("illustration_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("brush_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("brush_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("typography_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("typography_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("identity_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("identity_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("vector_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("vector_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orderBy($orderByString, 'desc')
->paginate(1);
As you can see, it's a bit redundant.
For every art type, I'm looking to get users by the role of "2", if their commstatus is equal to "1" or "$unavailable".
At first, I tried to shorten it by not adding "role" or "commstatus" at the end of each "where" clause, and at the bottom writing another $users = $users::where("role", "=", "2"), for example, but I can't seem to be able to find the right syntax that.
Is there any way to shorten this query?
You certainly shouldn't need to duplicate where('role', '=', 2)->where('commstatus', '=', $unavailableCheck) for every single type of art, as they're effectively ANDed conditions; and consider whereIn('commstatus', [$unavailableCheck, 1]) rather than having two equality checks.
Something like:
$users = User::where('role', '=', 2)
->whereIn('commstatus', [$unavailableCheck, 1])
->where("interface_art", '=', 1)
->orWhere("web_art", '=', 1)
->orWhere("illustration_art", '=', 1)
->orWhere("brush_art", '=', 1)
->orWhere("typography_art", '=', 1)
->orWhere("identity_art", '=', 1)
->orWhere("vector_art", '=', 1)
->orderBy($orderByString, 'desc')
->paginate(1);
As others have stated, your schema would likely benefit from refactoring towards normalization. However, I believe you can still refactor your existing query to be less redundant and more readable.
Laravel has the ability to handle Advanced Where Clauses which include nested parameter groupings. From the documentation:
The Closure will receive a query builder instance which you can use to set the constraints that should be contained within the parenthesis group.
With that in mind, you should be able to refactor the query like this:
$users = User::whereIn('commstatus', [$unavailableCheck, 1])
->where('role', 2)
->where(function ($query) {
$query->where("interface_art", 1)
->orWhere("web_art", 1)
->orWhere("illustration_art", 1)
->orWhere("brush_art", 1)
->orWhere("typography_art", 1)
->orWhere("identity_art", 1)
->orWhere("vector_art", 1);
})
->orderBy($orderByString, 'desc')
->paginate(1);
That will create a SQL query that does:
SELECT * FROM users
WHERE commstatus IN ($unavailableCheck, 1)
AND role = 2
AND (interface_art = 1 OR illustration_art = 1 OR ... etc)

syntax to join a table twice in laravel

I'm trying to join the below 3 tables. The issue is in the part where I want to join posts.id to the shares.post_id using 'AND' operator which is not correct. What is the correct syntax to do that ?
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', 'posts.user_id', '=', 'users.id' , 'AND' , 'posts.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
Try aliasing:
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', 'p1.user_id', '=', 'users.id' , 'AND' , 'p1.id', '=', 'shares.post_id')
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
You cannot join a table twice with no alias.
Although if you have 2 clauses in your join, I think you ought to do as in the documentation :
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', , function($join){
$join->on('p1.user_id', '=', 'users.id')
->on('p1.id', '=', 'shares.post_id')
})
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
Edit (with one posts join):
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', , function($join){
$join->on('posts.user_id', '=', 'users.id')
->on('posts.id', '=', 'shares.post_id')
})
->where('shares.user_id', '=', $myFriends)
->get();

Join in Laravel 5.1

I'm new to Laravel and I'm still working on my Project using this Framework. In my project, I use an SQL JOIN but I don't know how to retrieve/fetch data on the joined table.
Can anybody help me on this one?
I really appreciate your help. Thanks!
Here is a simple example!
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$users = \DB::table('users')
->join('contacts', 'contacts.user_id', '=', 'users.id')
->join('orders', 'orders.user_id', '=', 'users.id')
->select('users.id as id', 'contacts.phone as phone', 'orders.price as price')
//Where condition here if exist
->get();
$users will now have the array of values.Loop it in an array if necessary.
Eg:
forearch($users as $user)
{ $user->id;
$user->phone;
}
Like that.

Perform JOIN on three or four different tables in Laravel syntax

TABLES X(ID,A_ID)A(ID,B_ID),B(ID,C_ID),C(ID,D_ID)D(id,VALUE)
I want to retrieve the value of D table using laravel syntax on basis of X table Id and perform a JOIN with other tables.
Please post answers only in laravel syntax. In other format I can do. I am new to it so.
X::select('value')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
But it is not working. please provide me proper solution. Right now I am using PHP logic to get the value, rather than optimise the query.
There are many ways, but this is the basic:
$rows = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
doc
Try this:
DB::table('X')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->select('D.value')
->where('x.id', '=', $val)
->get();
$result = X::select('a.value', 'd.*')
->join('a', 'x.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
foreach ($result as $row) {
# code...
}

Categories