In my mySQL database I have 3 tables:
cocktails
ingredients
cocktail_ingredient
I can choose multiple ingredients ID, ex. [1,50,60,7,3]
I need to find the cocktails that I can prepare with ONLY this list of ingredients. It is possible to have less ingredients but they ALL must be presents in my ingredients list [1,50,60,7,3].
I declared my ManyToMany relationship, i created my Models, everything works, now I try to create my query with the ORM:
$ids = [1,50,60,7,3];
$coktails = Cocktail::has('ingredients','<=',count($ids))
->whereHas('ingredients',function($q) use ($ids){
$q->whereIn('ingredients.id',$ids);
})
->get();
I tried also:
$coktails = Cocktail::has('ingredients','<=',count($ids))
->whereHas('ingredients',function($q) use ($ids){
foreach ($ids as $id){
$q->where('ingredients.id',$id);
}
})
->get();
It's always a wrong count, i know my problem is in my whereHas Closure, but i can't find it.
Thank you
In your example, your target list of ingredients is [1,50,60,7,3]. Imagine you have a cocktail that requires ingredients [1, 2].
Based on your current logic:
has('ingredients', '<=', count($ids)) will match, because 2 <= 5, and
whereHas('ingredients', function($q) use ($ids) { $q->whereIn('ingredients.id', $ids); }) will match, because the subquery will return a record for id 1, and, by default, whereHas is only looking for at least one record.
So, based on that logic, cocktail with ingredients [1, 2] will be returned, and that is not what you want.
What you're really looking for is to make sure that you only get cocktails that don't have any ingredients that are not in your target list of ids. [1, 50] should match, since it is a subset, but [1, 2] should not, because ingredient 2 is not in the original set.
To handle this, you'll want to use a combination of the whereDoesntHave method and the whereNotIn method.
$ids = [1,50,60,7,3];
$cocktails = Cocktail::whereDoesntHave('ingredients', function($q) use ($ids) {
$q->whereNotIn('ingredients.id', $ids);
})
->get();
This statement is saying "get all the cocktails that don't have an ingredient that is not in this list of ingredients." [1, 50] will match, because it doesn't have any ingredients that are not in the list. However, [1, 2] will not match, because it does have an ingredient that is not in the list (2).
Related
I have the following two tables:
unotes unote_unit
====== ==========
id unote_id
notes unit_id
I am trying to retrieve unotes row whose related unit_id columns exactly match an input array.
So, I run following query:
$unote = UNote::whereHas('units', function ($query) use ($request) {
$query->whereIn('units.id', $request->unit_lists);
})
->withCount('units')
->having('units_count', '=', $u_list_count)
->get()
->pluck("id");
But, the problem with the above query is that even if it has just a single matching unit_id, it retrieves the data. For example I have following datasets:
unotes //with related unit_id
========
id = 3 //49865, 49866, 49867
id = 4 //49865, 49866
With above mentioned code, if I pass [49866,55555], it should return nothing but it returns ids 3 and 4, which contain one match but not all.
I have found similar question on Laracasts as well but running the query returns Cardinality violation: 1241 Operand should contain 2 column(s):
$unote = UNote::with('units')
->whereHas('units', function ($query) use ($request) {
$query->selectRaw("count(distinct id)")->whereIn('id', $request->unit_lists);
}, '=', $u_list_count)
->get()
->pluck("id");
I also found a similar question here, but seems it is too expensive.
Here is the dummy SQL to get started: http://sqlfiddle.com/#!9/c3d1f7/1
Because whereHas just adds a WHERE EXISTS clause to the query with your specified filters, a whereIn will indeed return true for any matches. One thing you could try is running a raw subquery to get a list of device IDs and compare it.
$search_ids = [49866, 49865];
sort($search_ids);
$search_ids = implode(",", $search_ids);
Unote::select("id")
->whereRaw(
"(SELECT GROUP_CONCAT(unit_id ORDER BY unit_id) FROM unote_unit WHERE unote_id = unotes.id) = ?",
[$search_ids]
)
->get()
->pluck("id");
Note, if you have soft deletes enabled you will also want to filter out soft deleted items in the subquery.
I have four tables technologies(id, name), tasks(id, name), requests(id, status_id, comment, task_id, created_at), technology_task(id, task_id, technology_id)
I want to get the requests whose status_id is 2 and join the tasks table which has specific technologies inserted in the technology_task pivot table. I want to group the result based on the date column created_at. Is this possible through Lravel model eloquent? I have tried the below method.
$this->model
->with('task', 'task.technologies')
->where('status_id', 2)->get()
->groupBy(function ($val) {
return Carbon::parse($val->created_at)->format('d-m-Y')
This is the Request model and this will return all the requests whose status_id is 2 along with the related task and technologies. But I only want the requests whose task have specific technologies, and I want to check that using something like ->whereIn('id', [1, 2, 3])->get()
How to achieve this using model? or do I need to use a custom query
Assuming $this->model is your Request model, and all your relationships are set correclty, you can use whereHas:
$technologyIds = [1, 2, 3];
$collection = $this->model
->with(['task', 'task.technologies'])
->whereHas('task.technologies', function($query) use ($technologyIds)
{
// here you can check using whereIn
$query->whereIn('technologies.id', $technologyIds);
})
->where('requests.status_id', 2)
->get();
If you want to use the groupBy inside the MySQL query, you will need to define what data should MySQL select when grouping. You can achieve that using aggregation functions. You weren't clear about the data you want to select, the code bellow should return:
the greatest id of the grouped rows,
unique comments concatenated with space
created_at
$technologyIds = [1, 2, 3];
$collection = $this->model
->with('task', 'task.technologies')
->whereHas('task.technologies', function($query) use ($technologyIds)
{
$query->whereIn('technologies.id', $technologyIds);
})
->where('requests.status_id', 2)
->groupBy('requests.created_at')
->selectRaw('
MAX(requests.id) AS id,
GROUP_CONCAT(DISTINCT requests.comment
ORDER BY requests.comment ASC SEPARATOR ' ') AS comments,
requests.created_at
')
->get();
Check out the docs to see how each aggregation functions works.
This is my controller code:
if ($request->genre && !$request->search) {
$genres = explode(',', $request->genre);
return Movie::with('genres')->whereHas('genres', function ($q) use ($genres) {
return $q->whereIn('id', $genres);
})->paginate(10);
}
My pivot table contains movie_id and genre_id. Both movies and genres tables do not contain reference of ID of each other.
I would need to select all movies from the table which have genres selected by the user.
For example, an array of id's = [1, 4, 9] and to select from pivot table movies that contain those genre ids.
Thanks in advance!
EDIT:
General problem was return inside callback and the way I handled data later on. Thanks for your answers, I appreciate it! :)
You did a wrong query, try the given query.
return Movie::with(['genres'])->whereHas('genres' ,function ($query) use ($genres){
$query->whereIn('id', $genres);
})->paginate(10);
I'm trying to get all the data from the parent that only has a child. Please see my code below.
$customers = Customer::with(['items' => function($query){
return $query->where('status', 2);
}])->get();
dd($customers);
But the code above returns all the customer. By the way, I'm using laravel 4.2.
Items Table:
Customer Table:
with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of one for every model in the collection.
has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.
e.g :
$users = Customer::has('items')->get();
// only Customer that have at least one item are contained in the collection
whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check.
e.g
$users = Customer::whereHas('items', function($q){
$q->where('status', 2);
})->get();
// only customer that have item status 2
Adding group by to calculating sum
this is another example from my code :
Customer::select(['customer.name', DB::raw('sum(sale.amount_to_pay) AS total_amount'), 'customer.id'])
->where('customer.store_id', User::storeId())
->join('sale', 'sale.customer_id', '=', 'customer.id')
->groupBy('customer.id', 'customer.name')
->orderBy('total_amount', 'desc')
->take($i)
->get()
in your case :
Customer::select(['customer_id', DB::raw('sum(quantity) AS total')])
->whereHas('items', function ($q) {
$q->where('status', 2);
})
->groupBy('customer_id')
->get();
whereHas() allow you to filter data or query for the related model in your case
those customer that have items and it status is 2
afetr getting data we are perform ->groupBy('customer_id')
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
select(['customer_id', DB::raw('sum(quantity) AS total')]) this will select customer id and calculate the sum of quantity column
You should use whereHas not with to check child existence.
$customers = Customer::whereHas('items', function($query){
return $query->where('status', 2);
})->get();
dd($customers);
I assume you already defined proper relationship between Customer and Item.
You should try this:
$customers = Customer::whereHas('items', function($query){
$query->where('status', 2);
})->get();
dd($customers);
Customer::select(['items.customer_id',DB::raw('count(items.id) AS total_qty')])
->join('items', 'items.user_id', '=', 'customer.customer_id')
->groupBy('items.customer_id')
->havingRaw('total_qty > 2')
->get();
OR
$data=DB::select("select `items`.`customer_id`, count(items.id) AS total_qty
from `customers`
inner join `items`
on `items`.`customer_id` = `customers`.`customer_id`
group by `items`.`customer_id` having total_qty >= 2");
correct table name and column name.
I have a project where a user can create conversations with other users. A conversation can belongsToMany users and user can belongsToMany conversations.
I now need to get the conversation in which two specific users participate.
I tried a combination of solutions using whereIn and I tried the following:
$c = Conversation::whereHas('users', function($q)
{
$q->whereIn('user_id', array(1,3));
})
->get();
Here the problem is that whereIn('user_id', [1,3]) gets records that contains EITHER 1 or 3. I need it to return records that contains BOTH 1 and 3.
Conversation Model
class Conversation extends Model {
public function users(){
return $this->belongsToMany('App\User');
}
}
User Model
class User extends Model {
public function conversations(){
return $this->belongsToMany('App\Conversation');
}
}
Tables
conversations:
id | subject
conversation_user:
id | user_id | conversation_id
Data from table conversation_user
Your newest edit makes a lot more sense, this is actually a very easy fix. whereHas takes two additional parameters where it's going to look for the count.
$users = [1, 3];
$c = Conversation::whereHas('users', function($q) use ($users)
{
$q->whereIn('user_id', $users);
}, '>', count($users) )
->get();
This will get all conversations where user's 1 and 3 have participated in, even if there are additional users that have participated in those conversations. If you want only the conversations with only users 1 and 3, change the > to an =.
Edit: I just realized your pivot table has an id column. This method may not work if your pivot table is going to have duplicates. For example, if you have user_id of 1 in there twice with the same conversation_id both times, it will return that conversation even though it technically only has 1 user. I suggest removing the id column and creating a composite primary key of user_id and conversation_id. If there is the possibility of duplicates, it might be safer to use lukasgeiter's solution.
You are currently querying conversations which either user 1 and/or 3 takes part in. To achieve what you want you need two whereHas calls:
$c = Conversation::whereHas('users', function($q)
{
$q->where('user_id', 1);
})
->whereHas('users', function($q)
{
$q->where('user_id', 3);
}
->get();
And if you have more than two users, add them in a loop:
$users = [1, 2, 3, 4, 5];
$c = Conversation::query();
foreach($users as $userId){
$c->whereHas('users', function($q) use ($userId)
{
$q->where('user_id', $userId);
});
}
$c = $c->get();
I hope this will help you...
$userIds = array(1,3);
$c = Conversation::whereHas('users', function($q) use ($userIds)
{
$q->whereIn('user_id', $userIds);
})
->get();