Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

massuncao's avatar

Laravel how to get values from a table with an id from another table

i am having some trouble getting results from a query. Let me explain. I have two tables, tasks and task_interactions. the table tasks stores the tasks, and the table task_interactions stores the history of the tasks, like a change of status, comments... The column task_id in the task_interactions table is the id in the table tasks. The table tasks has the following columns: id | project_id | task_type_id | related_block | created_by | assigned_to | start_date | due_date | task_priority | comments | status | created_at | updated_at. The status in this table will be deleted, right now i have no need for it.

The table taskinteractions has the following columns: id,task_id,status,comments,created_at

In the models i have created the following relationships:

Task Model:

public function taskinteractions()
{
    return $this->hasMany('App\Taskinteraction');
}

Taskinteraction Model:

public function tasks()
{
    return $this->belongsTo('App\Task');
}

And now the question: I want to get all the tasks assigned to the authenticated user with status (in the table taskinteractions) equal to "In Progress". I have tried a lot of different approaches with no good results. I am a newbie and so i believe that the solution might be simple, but i no idea how to do it.

0 likes
8 replies
Snapey's avatar

just a quick pointer, a relation that is belongsTo is a singular relationship so the relationship name should be singular also, e.g. taskinteraction belongs to task and not tasks

to your query

$tasks = $user->tasks()->whereHas('taskinteractions', function ($query) {
    $query->where('status', 'In Progress');
})-with('taskinteractions')->get();
massuncao's avatar

@Snapey .

Using your solution i get the following error: "Undefined variable: user".

I have tried different approaches to this problem, until now I have no good results. I believe the following approach is the easier one. Start by getting the task_id's where status is equal to the desired status. Then, use those task_id's to get the task data.

$inprogress = Taskinteraction::where('status','=','Unassigned')->get();

Echo $inprogress will return a collection, like this this:

[{"id":6,"task_id":47,"status":"Unassigned","comments":null,"created_at":"2017-04-28 13:18:13"},{"id":7,"task_id":48,"status":"Unassigned","comments":null,"created_at":"2017-04-28 13:20:09"},{"id":8,"task_id":49,"status":"Unassigned","comments":null,"created_at":"2017-04-28 13:24:38"}]

But, I only need the task_id, to be correct I only need an array of task_id’s to use in something like this:

$tasksinprogress = Task::where('assigned_to','=',auth::id())->whereIn('id', $inprogress)->get();

How do I extract the task_id property from this array of objects?

Another question: How do i query the table task_interactions to get the same result as the following query:

SELECT task_id FROM schema.task_interactions WHERE status = 'Unassigned';
Snapey's avatar

sorry but i assumed you would at least have the skills to work out that $user represents the user who's tasks your are trying to get.

massuncao's avatar

Ok, I'll try it later. I am not a developer, this is my first experience in coding and it only started one month ago. I'm discovering laravel and sometimes I see explanations that I assume are using plain code and not some of the framework's features. Thanks a lot for your help

Snapey's avatar
Snapey
Best Answer
Level 122

Sorry, did not mean to put you down. If you see something like that 'undefined' you need to think what was intended, where could the missing variable come from?

If you are getting tasks for the current user then you can initialise the $user with $user = Auth::user(); which gets the authenticated user, but if you only need the variable once ($user in this case), you can skip creating it and chain off the function.

So, the method I suggested could be changed to;

$tasks = Auth::user()->tasks()->whereHas('taskinteractions', function ($query) {
    $query->where('status', 'In Progress');
})-with('taskinteractions')->get();

... gets the tasks for the current user, but only the tasks that are 'In Progress'

massuncao's avatar

It's fine, i didn't take it that way. I just thought i should explain that i have little experience and so my questions might sound a bit dumb. Communication can be difficult when there's a big gap between the both ends.

I have tried your solution, and got an error, but i think this time it might be a design error. in my tasks table i am not using user_id, i am using a created_by and an assigned_to columns. The error says:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tasks.user_id' in 'where clause' (SQL: select * from `tasks` where `tasks`.`user_id` = 4 and `tasks`.`user_id` is not null and exists (select * from `task_interactions` where `tasks`.`id` = `task_interactions`.`task_id` and `status` = In Progress))

The query is looking for an user_id

Snapey's avatar

so when you define the relationship, something like belongTo uses convention to decide what the foreign key should be.

If you have a different name you can supply this as an additional parameter to the relationship

massuncao's avatar

Thanks a lot! Made a simple test and it's working... Also, thanks for the way you've answered the question, you could have give me the code, but no... you just gave me enough information to guide me. And I've learned something. :)

i have changed the method Tasks in the model User to this:

    public function Tasks()
    {
        return $this->hasMany('App\Task','assigned_to');
    }
1 like

Please or to participate in this conversation.