Collapse two collections to one in laravel - laravel

I need help with collapse my collections.
Its my code
public function showFriends(){
$request1 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',true]])
->orWhere([['user_id','=',Auth::user()->id],['accepted','= ',true]])
->get();
$request2 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where([['user_id','=',Auth::user()->id] , ['accepted','=',true]])
->get();
$all = collect($request1,$request2)->collapse();
return $all;
}
Problem is in $all variable because it return empty collection. Thank you for help.

Use the merge method on collections:
$all = $request1->merge($request2);

You can try this
$collection = collect($request1,$request2);
$collapsed = $collection->collapse();

You can also do this potentially with one query by utilizing union:
public function showFriends(){
$query1 = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.user_id')
->where([['friend_id', '=',Auth::user()->id],['accepted', '=',true]])
->orWhere([['user_id','=',Auth::user()->id],['accepted','= ',true]]);
$request = DB::table('friends_users')
->join('users', 'users.id', '=', 'friends_users.friend_id')
->where([['user_id','=',Auth::user()->id] , ['accepted','=',true]])
->union($query1)
->get();
return $request;
return $request->all(); // If you want an array rather than collection
}
The Laravel Documentation on unions:
The query builder also provides a quick way to "union" two queries
together. For example, you may create an initial query and use the
union method to union it with a second query

Related

how laravel join three tables with advance join clause using variable

Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();

Laravel distinct on join results not working in query builder

I have a posts table that has join query with 4 other tables. 3 of them are one to one relations but the 4th is one to many. I want the query to return only 1 row for each post. What i am trying so far is like this-
$query = DB::table('posts')
->select('posts.*',
'subcategories.subcategory_title_en',
'subcategories.subcategory_title_bn',
'categories.category_title_en',
'categories.category_title_bn',
'users.*',
'postimages.postimage_thumbnail'
)
->join('subcategories', 'subcategories.subcategory_id', '=', 'posts.subcategory_id')
->join('categories', 'categories.category_id', '=', 'subcategories.parent_category_id')
->join('users', 'users.id', '=', 'posts.user_id')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id');
$query->groupBy('posts.post_id');
echo $query->count();
exit();
I have currently 50 posts in database, but the query returns all rows for each postimages, more than 1 row for each post that is. I thought distinct would only show each post_id once? What am i missing here?
I would prefer if someone tells me how to do this with query builder. As this will have a lot of searching on different columns and i want it to be as fast as possible.
This is a simpler version -
$query = DB::table('posts')
->select('posts.*','postimages.postimage_thumbnail')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id')
->groupBy('posts.post_id');
echo $query->count();
exit();
The strange thing is the SQL query that is shown by laravel " select posts.*, postimages.postimage_thumbnail from posts inner join postimages on postimages.post_id = posts.post_id group by posts.post_id" works fine in mysql
You should use groupby
Try this code
$query = DB::table('posts')
->select('posts.*',
'subcategories.subcategory_title_en',
'subcategories.subcategory_title_bn',
'categories.category_title_en',
'categories.category_title_bn',
'users.*',
'postimages.postimage_thumbnail'
)
->join('subcategories', 'subcategories.subcategory_id', '=', 'posts.subcategory_id')
->join('categories', 'categories.category_id', '=', 'subcategories.parent_category_id')
->join('users', 'users.id', '=', 'posts.user_id')
->join('postimages', 'postimages.post_id', '=', 'posts.post_id')->groupBy('posts.post_id');
In config/database.php at "mysql" change : 'strict' => true, to false

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Eloquent Complex Joins

i have made a scope
public function scopeCollaborative($query){
return $query->leftJoin('collaborative', function($join){
$join->on('imms.phone2', '=', 'collaborative.phone')
->orOn('imms.phone', '=', 'collaborative.phone')
->where('collaborative.user_id', '=', App('CURUSER')->id);
});
}
in Query log this scope adds:
left join `cs_collaborative` on
`cs_imms`.`phone2` = `cs_collaborative`.`phone` or
`cs_imms`.`phone` = `cs_collaborative`.`phone` and
`cs_collaborative`.`user_id` = 3
but i need to have:
left join `cs_collaborative` on
(`cs_imms`.`phone2` = `cs_collaborative`.`phone` or
`cs_imms`.`phone` = `cs_collaborative`.`phone`) and
`cs_collaborative`.`user_id` = 3
i didn't found a good solution, JoinClause have functions: On, orOn, where, orWhere.
but non of all can take function as input and to group query...
someone ideals?
Laravel doesn't let you build such join clause, so you need this to make it work:
public function scopeCollaborative($query){
return $query->leftJoin('collaborative', function($join){
$join->on('imms.phone2', '=', 'collaborative.phone')
->where('collaborative.user_id', '=', App('CURUSER')->id)
->orOn('imms.phone', '=', 'collaborative.phone')
->where('collaborative.user_id', '=', App('CURUSER')->id);
});
}
you can consider to use Doctrine ORM is more powerfull, bur less easy in the beginning...

A JOIN With Additional Conditions Using Query Builder or Eloquent

I'm trying to add a condition using a JOIN query with Laravel Query Builder.
<?php
$results = DB::select('
SELECT DISTINCT
*
FROM
rooms
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND ( bookings.arrival between ? and ?
OR bookings.departure between ? and ? )
WHERE
bookings.room_type_id IS NULL
LIMIT 20',
array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10')
);
I know I can use Raw Expressions but then there will be SQL injection points. I've tried the following with Query Builder but the generated query (and obviously, query results) aren't what I intended:
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function ($join) {
$join->on('rooms.id', '=', 'bookings.room_type_id');
})
->whereBetween('arrival', array('2012-05-01', '2012-05-10'))
->whereBetween('departure', array('2012-05-01', '2012-05-10'))
->where('bookings.room_type_id', '=', null)
->get();
This is the generated query by Laravel:
select distinct * from `room_type_info`
left join `bookings`
on `room_type_info`.`id` = `bookings`.`room_type_id`
where `arrival` between ? and ?
and `departure` between ? and ?
and `bookings`.`room_type_id` is null
As you can see, the query output doesn't have the structure (especially under JOIN scope). Is it possible to add additional conditions under the JOIN?
How can I build the same query using Laravel's Query Builder (if possible) Is it better to use Eloquent, or should stay with DB::select?
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function($join)
{
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on('arrival','>=',DB::raw("'2012-05-01'"));
$join->on('arrival','<=',DB::raw("'2012-05-10'"));
$join->on('departure','>=',DB::raw("'2012-05-01'"));
$join->on('departure','<=',DB::raw("'2012-05-10'"));
})
->where('bookings.room_type_id', '=', NULL)
->get();
Not quite sure if the between clause can be added to the join in laravel.
Notes:
DB::raw() instructs Laravel not to put back quotes.
By passing a closure to join methods you can add more join conditions to it, on() will add AND condition and orOn() will add OR condition.
If you have some params, you can do this.
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function($join) use ($param1, $param2)
{
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on('arrival','=',DB::raw("'".$param1."'"));
$join->on('arrival','=',DB::raw("'".$param2."'"));
})
->where('bookings.room_type_id', '=', NULL)
->get();
and then return your query
return $results;
You can replicate those brackets in the left join:
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND ( bookings.arrival between ? and ?
OR bookings.departure between ? and ? )
is
->leftJoin('bookings', function($join){
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on(DB::raw('( bookings.arrival between ? and ? OR bookings.departure between ? and ? )'), DB::raw(''), DB::raw(''));
})
You'll then have to set the bindings later using "setBindings" as described in this SO post:
How to bind parameters to a raw DB query in Laravel that's used on a model?
It's not pretty but it works.
The sql query sample like this
LEFT JOIN bookings
ON rooms.id = bookings.room_type_id
AND (bookings.arrival = ?
OR bookings.departure = ?)
Laravel join with multiple conditions
->leftJoin('bookings', function($join) use ($param1, $param2) {
$join->on('rooms.id', '=', 'bookings.room_type_id');
$join->on(function($query) use ($param1, $param2) {
$query->on('bookings.arrival', '=', $param1);
$query->orOn('departure', '=',$param2);
});
})
I am using laravel5.2 and we can add joins with different options, you can modify as per your requirement.
Option 1:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);//you add more joins here
})// and you add more joins here
->get();
Option 2:
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')// you may add more joins
->select('users.*', 'contacts.phone', 'orders.price')
->get();
option 3:
$users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->leftJoin('...', '...', '...', '...')// you may add more joins
->get();
For conditional params we can use where,
$results = DB::table('rooms')
->distinct()
->leftJoin('bookings', function($join) use ($param)
{
$join->on('rooms.id', '=', 'bookings.room_type_id')
->where('arrival','=', $param);
})
->where('bookings.room_type_id', '=', NULL)
->get();
There's a difference between the raw queries and standard selects (between the DB::raw and DB::select methods).
You can do what you want using a DB::select and simply dropping in the ? placeholder much like you do with prepared statements (it's actually what it's doing).
A small example:
$results = DB::select('SELECT * FROM user WHERE username=?', ['jason']);
The second parameter is an array of values that will be used to replace the placeholders in the query from left to right.
My five cents for scheme LEFT JOIN ON (.. or ..) and (.. or ..) and ..
->join('checks','checks.id','check_id')
->leftJoin('schema_risks', function (JoinClause $join) use($order_type_id, $check_group_id, $filial_id){
$join->on(function($join){
$join->on('schema_risks.check_method_id','=', 'check_id')
->orWhereNull('schema_risks.check_method_id')
;
})
->on(function($join) use ($order_type_id) {
$join->where('schema_risks.order_type_id', $order_type_id)
->orWhereNull('schema_risks.order_type_id')
;
})
->on(function($join) use ($check_group_id) {
$join->where('schema_risks.check_group_id', $check_group_id)
->orWhereNull('schema_risks.check_group_id')
;
})
->on(function($join) use($filial_id){
$join->whereNull('schema_risks.filial_id');
if ($filial_id){
$join->orWhere('schema_risks.filial_id', $filial_id);
}
})
->on(function($join){
$join->whereNull('schema_risks.check_risk_level_id')
->orWhere('schema_risks.check_risk_level_id', '>' , CheckRiskLevel::CRL_NORMALLLY );
})
;
})

Resources