How do I group the results of a belongsToMany() relationship that has withPivot() values?
groupBy() produces an SQL error and distinct() does work if there is no withPivot() data.
But I need the pivot data in the results.
// Relationship: room to chores
public function chores(){
return $this->belongsToMany(
Chore::class,
'maps'
)
->withPivot('id', 'room_id', 'chore_id', 'person_id')
->groupBy('chores.id');
}
SQL ERROR
SELECT list is not in GROUP BY clause and contains nonaggregated column 'pivot.maps.room_id' which is not functionally dependent on columns in GROUP BY clause
You could use a SELECT clause to specify which columns should be included in the results. This allows you to specify which columns should be included in the results, while still grouping the results by the chores.id column.
public function chores()
{
return $this->belongsToMany(Chore::class, 'maps')
->select('chores.*', 'pivot.id', 'pivot.room_id', 'pivot.chore_id', 'pivot.person_id')
->withPivot('id', 'room_id', 'chore_id', 'person_id')
->groupBy('chores.id');
}
Related
I want to select distinct results from one table, the structure is following:
id, user_id, category_id, last_answered_question_id
I have relation for last_answered_question_id called: lastAnsweredQuestion()
My query is:
Answer::select('category_id')->where(['user_id' => $userId])->distinct()->get();
This query works as expected but in this case I can't call relation $answer = lastAnsweredQuestion.
Is there any known way to get distinct items and call relation lastAnsweredQuestion() anyway?
I have two tables which are named as students and cities. students table has a primary key which is related to the cities table.
Students table:
In the frontend, I would like to have the data of the cities table with these requirements:
The data of the students table must be grouped by city_id
If there are more than one records with the same city_id in the students table, select only the latest record of the group.
Search in the latest records and select only students who are inactive.
Here is the relationship function of the city model:
public function student()
{
return $this->hasOne(Student::class, 'city_id', 'id')->orderByDesc('id');
}
This is my controller function:
$data = City::whereHas('student', function($query){
$query->where('is_active', 0)
})->with('student')->get();
Expected result: Considering the sample data, the query must return nothing.
Current result: It returns the third row as there is an inactive student record in the second row. So in this case where condition doesn't work properly.
I can get expected result with this SQL query:
select *
from students s
where id = (select max(t2.id)
from students s2
where s.city_id = s2.city_id) AND is_active = '0';
How can I fix this logical error?
What about something like this?
public function inactiveStudent()
{
return $this->hasOne(Student::class, 'city_id', 'id')
->where('is_active', 0)
->orderByDesc('id');
}
$data = City::whereHas('inactive_student')
->with('inactive_student')
->get();
I'm not certain if it's inactive_student or inactiveStudent when you do the query.
I have a model User that has many Orders. Orders have many products, with the pivot table order-product. I don't want to preload and iterate through the orders if at all possible.
I need to return users where
signed_date === true on User
order_date on Order is after signed_date on User
order-product shows product hasn't been paid
I am failing on number 2.
In the following code, the first query within whereHas is wrong. I don't know how to reference the signed date of the user from within the where has. If I was iterating through users in a collection I could do something like ($query) use $user, but how do I do this without preloading all the users?
return User::whereNotNull('signed_date')
->whereHas('orders', function ($query) {
$query->where('order_date', '<=', 'user.signed_date');
$query->whereHas('products', function ($q) {
$q->where('paid', false);
});
})
->get(['id','fname','lname', 'title', 'signed_date']);
I would like to use eloquent if possible. If that is not possible, I would be happy for tips in solving this problem using the query builder/sql.
The Eloquent query builder has a special function called whereColumn('a', '<=', 'b') to compare columns instead of a column against a value. Using this function instead of a normal where() is necessary because of the way the query builder builds the actual query. You need to let the query builder know that you are going to pass a column name instead of a value for proper escaping and formatting of the query string.
Anyway, it seems you can also pass column names prefixed with a table name to the function, allowing you to compare columns across tables:
$query->whereColumn('orders.order_date', '<=', 'users.signed_date')
This works because you are using whereHas() in your query. Your query basically gets translated to:
SELECT id, fname, lname, title, signed_date
FROM users
WHERE signed_date NOT NULL
AND EXISTS (
SELECT 1
FROM orders
WHERE orders.order_date <= users.signed_date
AND EXISTS (
SELECT 1
FROM products
WHERE paid = 0
)
)
It might actually be not necessary at all to use the table name together with the column name in whereColumn(). But in case you'll ever add a column named the same on another table, the query might break - so IMHO it is good practice to use the table name in custom queries.
By the way, the reason this will not work together with with('relationship') is that this function results in an additional query and you obviously cannot compare columns across queries. Imagine the following:
Order::with('user')->take(5)->get();
It will be translated into the following:
SELECT *
FROM orders
LIMIT 5
SELECT *
FROM users
WHERE id IN (?, ?, ?, ?, ?)
where the five ? will be the user_ids of the orders. If the first query returns multiple rows with the same user_id, the amount of rows fetched from the users table gets reduced of course.
Note: All the queries are only examples. Might be that the query builder builds different queries based on the database type and/or escapes them differently (i.e. column names in backticks).
Following is my query
$user = User::select(['uuid','name','about'])->with(['education','work'])->first();
this returns empty data for relationship education and work,
but if I remove select function from query I am getting data in relationship and it also returns all columns of user table which I don't want.
how can solve this problem
The problem is that relationships (with(...)) execute an additional query to get the related results. Let's say you have one to many relationship where users have many works. User::with('work')->find(1) will then execute these 2 queries:
select user where id = 1 and select works where user_id = 1.
So basically in order to be able to execute the second query (fetch relationship data) you need to include id (or whichever column you're referencing) in you select statement.
Fix:
$user = User::select(['uuid','name','about', 'id'])->with(['education','work'])->first();
Same principle in different forms applies to all relationships. For example in the inverse of hasMany which is belongsTo you would need to select the foreign key (for example user_id).
I'm joining two tables that both have tag_id (i.e. album.tag_id and photo.tag_id)
function GetJoined()
{
return Album::join('photos', 'photos.tag_id', '=', 'albums.tag_id')
}
but I don't want that resulting query to keep reminding me that this was a join from two tables
GetJoined()->where('tag_id', 1);
will be all like
'tag_id' in where clause is ambiguous
Any suggestions?