Monday, March 13, 2017

Eloquent Relationship in Laravel ManytoMany Through

I have three classes in my Laravel project : City, Company and activity. So, City has many Company, and Comapany belongs to a city; and then i have that Company may have one or many acitivities; What i'm asking about is how can I have all activities in a city.

City Model:

public function companies()
    {
        return $this->hasMany('App\Models\Company');
    }

Company Model:

public function city()
    {
        return $this->belongsTo('App\Models\City');
    }

public function activities()
    {
        return $this->belongsToMany('App\Models\Activity');
    }

Activity Model:

public function companies()
    {
        return $this->belongsToMany('App\Models\Company');
    }

Thanks for everything. Best Regards.



via midodesign

Advertisement