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

bottelet's avatar

Get information from 3 diffrent tables to one page

Hello Laracasters!

This is a follow up on my previous question, which seemed like noone had a answer to unfortunately (https://laracasts.com/discuss/channels/code-review/only-gets-the-correct-name-and-id) I will instead try to explain what i want to achieve with out the code.

I have a users table

  • ID
  • Name
  • Email
  • Number
  • Password
  • Rember_Token

Tasks Table:

  • ID
  • Title
  • Description
  • fk_user_id_assign
  • fk_user_id_created
  • fk_client_id

And then the clients table:

  • ID
  • Name
  • Email
  • Number
  • fk_user_id

On the clients Show page, i wanna display the tasks the client currently have, in a list with the title, but here comes what i can't get working, besides the title i want what user is assigned to the Task aswell. How can i achieve this?

So Clients profile page > List of tasks with Users assigned to the task, and the email and number of the user(Not client)

0 likes
10 replies
jlrdw's avatar

Have you watched any laracast on relations or dug into the documentation on one to many and many to many and really all types of relations? Jeffrey includes some examples in the docs and there is also tutorials included in the docs.

bottelet's avatar

I've been reading the Docs ofc, but only seen 2 or 3 videos, as i dont really like learning with video. But haven't been able to get it to work yet unfortunately

jlrdw's avatar

Look at has many through, I thanks that would get you going in the right direction.

Snapey's avatar

start with some eloquent training. you will need to learn how to tell eloquent the name for the foreign keys since the assumed column name is tablename_id and the tables own column name is expected to be id not ID. If

There are many free videos on here and YouTube so I suggest you at least try those to start with. they may not be your preferred way by you can learn a lot in a short space in time.

bottelet's avatar

Okay i think i might be on the right track here currently i have:

return $this->hasManyThrough('App\Tasks', 'App\User', 'id', 'fk_client_id');

in my Client model

Which outputs the following SQL :

string(126) "select `tasks`.*, `users`.`id` from `tasks` inner join `users` on `users`.`id` = `tasks`.`fk_client_id` where `users`.`id` = ?"

But i would need the SQL string to say tasks.fk_client_id = ?, I'm fairy certain, how would i do that?

bottelet's avatar

@Snapey I know what you mean, This is my eloquent training though, I'm just stuck at a problem. And i dont like learning by following a video and just "copy" the code written there. This is just how i preffer to do it. I know Laracast has great explanation, as i've seen 2 or 3, but i just understand it better this way.

Snapey's avatar
Snapey
Best Answer
Level 122

If you can use the convention for ids and foreign keys then it will save further hassle

No need to use has manyThrough since most times you will want all tables anyway.

So you have

  • client that has many tasks
  • task that belongs to a client
  • task that belongs to a user
  • user that has many tasks assigned
  • user that has many tasks created

in your Client model

    protected $primaryKey ='ID';

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

in your Task model

    protected $primaryKey ='ID';

    public function client()
    {
        return $this->belongsTo('app\Client','fk_client_id');
    }
    
    public function taskCreator()
    {
        return $this->belongsTo('app\User','fk_user_id_created');
    }

    public function taskAssignedTo()
    {
        return $this->belongsTo('app\User','fk_user_id_assign');
    }

in your User model

    protected $primaryKey ='ID';

    public function tasksAssigned()
    {
        return $this->hasMany('app\Task','fk_user_id_assign');
    }

    public function tasksCreated()
    {
        return $this->hasMany('app\Task','fk_user_id_created');
    }

.... or something like that..

then to get the data for your page, you will be able to eager load the relationships, like

    $client = Client::with('tasks','task.assignedTo')->findOrFail(1);

    return view('clientTasks')->with(compact('client'));

and in your view you will have a client object with a collection of client tasks, and each task will have a child element of the user assigned.

    @foreach($client->tasks as $task)
        <td>{{ $task->Title}}</td>
        <td{{ $task->user->Name }}</td>
    @endforeach
2 likes
bottelet's avatar

I think im very close now, i tested it and changed a couple of things, but i currently have the

          $client = Client::with('Tasks','Tasks.assignedTo')->findOrFail(1);
          return view('clients.show')->with(compact('$client'));
  @foreach($client->tasks as $task)
        <td>{{ $task->Title}}</td>
        <td>{{ $task->user->Name }}</td>
    @endforeach

But im getting an undefined variable error

ErrorException in 2b1e26936a355e77d57a52baf5ea92f4 line 20:
Undefined variable: client (View: C:\xampp\htdocs\laravelcms\resources\views\clients\show.blade.php)

I'm almost starting to feel bad for asking for so much help, dont hope I'm too big of a burden :)

UPDATE-----------------

A bit of googling and i removed the '$' from Compact('$client')); And that worked, but i am unfortunately still not getting anything output in my view

SECOND UPDATE-------------

  @foreach($client->tasks as $task)
        <td>{{ $task->title}}</td>
    @endforeach

I got the above to work but not when useing

  <td>{{ $task->user->name }}</td
Snapey's avatar

in your controller, dd($client); and then you can review the structure of the $client object and its relationships.

check the spelling and capitalisation of the fields. For instance, earlier you had 'Name' as the username, but here 'name'

(sorry for the mistake with compact('$client')) - I have been back and edited the response in case someone else copies it)

bottelet's avatar

Got it to work BIIIG Thanks to everyone and really thanks too you @Snapey

Also i dont know how to say this without looking to dumb, but i think it might have worked from the begining but i we're using Bootstrap Popover to display the information, and thats why i coulndt show the correct data to start with and only the first ID.

Please or to participate in this conversation.