Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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.

0

Thank you.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

bpattan bpattan Joined 17 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.