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

Hi Donny5300,

If you using the Companies_Schedules as a pivot table of companies and schedules you can make it using your models you can tell to them how relate with the others tables:

class Company extends Eloquent{
    protected $table = "companies";
    public function schedules{
        return $this->hasMany('Schedule'); //you can specity more options to this method.
    }
}

class Schedule extends Eloquent{
    protected $table = "schedules";
    public function company{
        return $this->belongsTo('Company'); //you can specify more options to this method
    }
}

class CompanySchedule extends Eloquent{
    protected $table = "companies_schedules";
    public function company{
        return $this->belongsTo('Company'); //you can specify more options to this method
    }
    public function schedule{
        return $this->belongsTo('Schedule'); //you can specify more options to this method
    }

}

You can find more examples in the official documentation of Laravel, you can use pivot tables.

If anybody can get us more example of this question because I could be wrong.

Thanks so much.

Hope it helps you.

Last updated 2 years ago.
0

But how does i have to work it out in the controller? In doctrine EM i knew it works with getting both repo's and then attach them to each other

Last updated 2 years ago.
0

Outside of the Model you ned to call the methods of the models, in example:

class SchedulesController extends BaseController {
    public function getCompany{
        return Schedule::has('company')->get();
    }
    
}

And in the Routes.php you can use as follow:

Route::get('companyschedules', 'SchedulesController@getCompany'){

});

Hope it helps you..

Last updated 2 years ago.
0

I know how to get the results, but what i dont get is how to insert the company and schedule in the joined table i created for it. In normal PHP i could do

$sql = "INSERT INTO company_schedule ('user_id', 'company_id') VALUES (1 , 15); $execute = mysql_query($sql);

But how does i do that in Eloquent?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Donny5300 donny5300 Joined 3 May 2014

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.