Untested and freehand, but should get you pointed in the right direction.
class Time_Tracker extends Eloquent {
public function Module()
{
/*
- dest model name, local_key, remote_key
*/
return $this->hasOne('Module', 'time_tracker_module_id', 'module_id');
}
public function Task()
{
/*
- dest model name, local_key, remote_key
*/
return $this->hasOne('Task', 'time_tracker_task_id', 'task_id');
}
}
class Module extends Eloquent {
public function TimeTracker()
{
/*
- dest model name, local_key, remote_key
*/
return $this->belongsTo('Time_Tracker', 'module_id', 'time_tracker_module_id');
}
}
class Task extends Eloquent {
public function TimeTracker()
{
/*
- dest model name, local_key, remote_key
*/
return $this->belongsTo('Time_Tracker', 'task_id', 'time_tracker_task_id');
}
}
And to query,
/* get data from Time_Tracker */
$data = Time_Tracker::find(1)->with('Module')->with('Task')->get();
/* if have Module ID and want Task and TimeTracker */
$data = Module::find(1)->with('TimeTracker', 'TimeTracker.Task')->get();
/* if have Task ID and want Module and TimeTracker */
$data = Task::find(1)->with('TimeTracker', 'TimeTracker.Module')->get();
You might not need remote key, if the remote_key is the primary key for the remote table.
Also might want to use hasMany instead of hasOne.
And there is a whole bunch of relational functions that could be used to build model relationships, see http://laravel.com/docs/4.2/eloquent for more on that.
Hope that helps.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community