php - Laravel Eloquent one to many -


i have 2 models, company , jobs. company can have many jobs.

job model:

class job extends \eloquent {      public function company() {         return $this->belongsto('company');     } } 

company model:

class company extends \eloquent {      public function jobs() {         return $this->hasmany('job');     } } 

if following, want $job object have both job & company objects in same way if did sql join such as:

select *  `jobs` join company on `company_id` = company.id jobs.`id` = 156; 

instead, if this

$job = job::find($id); var_dump($job); exit; 

$job has job.

if this:

$job = job::find($id)->company; var_dump($job); exit; 

i company.

how $job equivalent of sql join?

you should able use ->with() function of laravel return model job , it's associated model company:

$job = job::with("company")->find($id); 

you should able access fields in job model would:

$job->field_1;  $job->field_2;  ... 

and company model's fields:

$job->company->field_1; $job->company->field_2; ... 

i'm not 100% familiar usage in laravel 5, further information can found here:

laravel documentation - eager loading


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -