php - Laravel: returning results from multiple related tables using eloquent -
i'm using laravel 4 , in particular i'm looking answer uses eloquent orm.
i have table "tasks" containers client_id , user_id assigned each row.
client_id refers client on "clients" table , user_id refers user on "users" table.
what want do: show tasks , display "clients" name , "users" first_name
so result in (blade) view:
@foreach($tasks $task) <tr> <td>{{ $task->user->first_name }}</td> <td>{{ $task->client->name }}</td> <td>{{ $task->description }}</td> </tr> @endforeach
the above view spits out $task->client->name fine unfortunately shows "trying property of non-object" when add line $task->user->first_name
my controller looks this:
$tasks = task::with(array('user', 'client'))->get(); return view::make('index', compact('tasks'));
as understand models make difference too, models 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 make work? i've been scratching head while - note i'm not database relationship pro simpler explanation better :)
i worked through , learned quite few things myself. did setup many many relationship between users
, clients
, created pivot table handling relationship called tasks
stores description each task.
it type here, can check out code @ http://paste.laravel.com/fpv
Comments
Post a Comment