Please format your code by putting 3 backticks ``` on a line before and after each code block
can't see where you might be asking for anything to do with tasks.id ?
How is $query set? Is this code inside a scope? What model is being scoped?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a Task model which has a M:M relationship setup called transfers. This links Task & User models through a pivot table called transfer_task. The following code works fine and displays just the tasks that have been transferred to the current user
$current_user_id = \Auth::user()->id;
$query->orWhereHas('transfers', function ($query) use($current_user_id) {
$query->where('users.id', $current_user_id);
});
I am trying to grab just the latest entry from the pivot table so I only have the person it was transferred to last in my collection. I need the full transfer history (for elsewhere in the application) which is the reason I have M:M rather than 1:M.
I have tried the following but I get an error message Unknown column tasks.id in where clause
$current_user_id = \Auth::user()->id;
$query->orWhereHas('transfers', function ($query) use($current_user_id) {
$query->latest()->first();
$query->where('users.id', $current_user_id);
});
I have basically stuck $query->latest()->first(); before $query->where('users.id', $current_user_id); in an attempt to remove the earlier transfers.
Does anyone know how I can resolve this?
An easy fix would be to add a secondary relationship called transfer() which is a belongsTo relationship :)
Please or to participate in this conversation.