9

I'm using Laravel 4 and in particular I'm looking for an answer that uses eloquent ORM.

I have a table "tasks" which containers a client_id and a user_id assigned to each row.

client_id refers to a client on a "clients" table and user_id refers to a user on a "users" table.

What I want to do: show all tasks and display the "clients" name and "users" first_name

So the result would look like this in my (blade) view:

@foreach($tasks as $task)
    <tr>
        <td>{{ $task->user->first_name }}</td>
        <td>{{ $task->client->name }}</td>
        <td>{{ $task->description }}</td>
    </tr>
@endforeach

The above view spits out the $task->client->name perfectly fine but unfortunately shows a "Trying to get property of non-object" when I add the line $task->user->first_name

My controller looks like this:

$tasks = Task::with(array('user', 'client'))->get();
return View::make('index', compact('tasks'));

As I understand it my models make a difference too, so my models look like this:

class Task extends Eloquent {
    protected $guarded = array();
    public static $rules = array();

    public function client() {
        return $this->belongsTo('Client');
    }

    public function user() {
        return $this->belongsTo('User');
    }
}

And:

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function task()
    {
        return $this->hasMany('Task');
    }
}

And:

class Client extends Eloquent {
    public function projects(){
        return $this->hasMany('Project', 'client_id');
    }
}

Any ideas on how to make this work? I've been scratching my head for a while - also note I'm not a database relationship pro so the simpler the explanation the better :)

2
  • It would seem to me that your Tasks table is a pivot table in a many-to-many relation. You should probably use Eloquent's belongsToMany() method to link Client and User directly. See this for info on how to add additional fields to your pivot table. Commented Jul 31, 2013 at 8:36
  • Hi @cuewizchris I took a look at the documentation and I can't work out how to translate their example of User/Role/Pivot to my needs. Are you saying I should change my hasMany() to toMany()? I'm a bit lost here. Commented Jul 31, 2013 at 9:39

2 Answers 2

2

I just worked through this and learned quite a few things myself. What I did was setup a many to many relationship between users and clients and created a pivot table for handling the relationship called tasks which also stores the description for each task.

It was too much to type here, but you can check out my code at http://paste.laravel.com/Fpv

Sign up to request clarification or add additional context in comments.

4 Comments

Absolutely genius. Thank you this is exactly what I was looking for. However (and I admit this is beyond the scope of my question) - it gets really complicated / breaks when you try add pagination and/or orderBy (e.g. order by created_at DESC) statements to your eloquent query in your controller :) obviously with the orderBy created_at it's trying to get the user's created_at date not the task's created_at date.
I've tried a few things, but it always seems to fail when I try to paginate or orderBy when eager loading. I am assuming the best way to do this would be to use array_slice for paginate and whatever sorting function would be appropriate to you.
It is never "too much to type here". I can't resolve your link.
downvote because an answer should include a complete answer - not just a suggestion and a link (especially when the link is a broken one)
0

Many-to-many relationships can be done like this with Eloquent:

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function client()
    {
        return $this->belongsToMany('Client', 'tasks', 'client_id')->withPivot('description');
    }
}

and the inverse relationship...

class Client extends Eloquent {
    public function users()
    {
        return $this->belongsToMany('User', 'tasks', 'user_id');
    }
}

Haven't tested this, but it should be correct.

1 Comment

I apologise for my lack of knowledge and the number of questions, but what would my eloquent query look like in my controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.