Laravel: Nested relationship method - laravel

I have the following models:
users
id
name
organisation_members
id
organisation_id (FK to organisations)
user_id (FK to users)
unqiue(organisation_id, user_id)
organisations
id
name
A user can be a member to many organisations.
My models are written as follows
class User extends Model {
public function organisationMembers(): HasMany
{
return $this->hasMany(OrganisationMember::class);
}
public function organisations()
{
// TODO: how do I do this one
}
}
class OrganisationMember extends Model {
public function organisation(): BelongsTo
{
return $this->belongsTo(Organisation::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
class Organisation extends Model {
public function organisationMembers(): HasMany
{
return $this->hasMany(organisationMember::class);
}
}
How can I write a relationship method in the User model for organisations?

Thanks to #matiaslauriti comment the answer is:
class User extends Model {
public function organisationMembers(): HasMany
{
return $this->hasMany(OrganisationMember::class);
}
public function organisations()
{
return $this->belongsToMany(Organisation::class, 'organisation_members');
}
}

Related

How to elegantly use a remote one-to-many like association on a many-to-many in Laravel

For example, there are four models as follows.
class User extends Authenticatable
{
public function orders(): MorphMany
{
return $this->morphMany(Order::class, 'orderable');
}
}
class Partner extends Model implements Authenticatable
{
public function orders(): MorphMany
{
return $this->morphMany(Order::class, 'orderable');
}
}
class Order extends Model
{
public function orderable(): MorphTo
{
return $this->morphTo();
}
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class)->withPivot('amount', 'price');
}
}
class Project extends Model
{
public function orders(): BelongsToMany
{
return $this->belongsToMany(Order::class)->withPivot('amount', 'price');
}
}
User、Partner and Order are one to many polymorphic relations。
Order and Project are many to many relations。
How to elegantly get all projects enrolled by a given user?
Thanks!

laravel relationship between 5 tables

I have 5 tables User, Profile, Address,State,City .Need to create relationship between tables.Address has State id , City id and Profile id in the table.Profile has User Id in the table.City has State Id in the table. How to write relationship between table
class City extends Model
{
public function state() {
return $this->belongsTo('App\State');
}
public function addresses() {
return $this->hasMany('App\Address');
}
}
class State extends Model
{
public function cities() {
return $this->hasMany('App\City');
}
public function addresses() {
return $this->hasMany('App\Address');
}
}
class Profile extends Model
{
public function address() {
return $this->belongsTo('App\Address');
}
public function user() {
return $this->belongsTo('App\User');
}
}
class Address extends Model
{
public function profile() {
return $this->belongsTo('App\Profile');
}
public function city() {
return $this->belongsTo('App\City');
}
public function state() {
return $this->belongsTo('App\State');
}
}
// users table
public function profile(){
return $this->hasOne('App\Profile');
}
Generally your model design is true, i have edited a few parts. Try the codes below.
class City extends Model
{
public function state()
{
return $this->belongsTo('App\State');
}
public function addresses()
{
return $this->hasMany('App\Address');
}
}
class State extends Model
{
public function cities()
{
return $this->hasMany('App\City');
}
public function addresses()
{
return $this->hasMany('App\Address');
}
}
class Profile extends Model
{
public function addresses()
{
return $this->hasMany('App\Address');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
class Address extends Model
{
public function profile()
{
return $this->belongsTo('App\Profile');
}
public function city()
{
return $this->belongsTo('App\City');
}
public function state()
{
return $this->belongsTo('App\State');
}
}
class User extends Model
{
public function profile()
{
return $this->hasOne('App\Profile');
}
}
By the way, Laravel relationships add default keys according to your method names. If you have problem about it you can find info from official documents. For example:
$this->belongsTo('App\Model', 'foreign_key', 'other_key');
as #mustafa.akcoban says...
When you use belongsTo Eloquent will work as follow
$this->belongsTo('App\City', 'foreign_key', 'other_key');
// foreign_key = is the primary key in the related model, by default 'id' for Eloquent
// other_key = is the field in the current model that contains the id of the other model, by default othermodelname_id for Eloquent
// For eg. 'App\City', 'id', 'city_id'
When you use hasMany Eloquent works as follow
$this->hasMany('App\Model', 'currentmodel_id', 'primary_key');
// currentmodel_id = is the field that contains the current model primary key in the related model
// primary_key = is the current primary key model that will be in the other model, by default id for Eloquent
// For eg. 'App\City', 'state_id', 'id'
Remember you can or can't use second and third parameter, if something is wrong Laravel dump will tell you what column was not found in the table, and you will can fix.
Please try and practice this, and let me know how it works :)

Get resources with hasManyThrough a model using BelongsTo

I have the following models:
User:
- id
- name
Location:
- id
- name
- region_id
table: user_location
- user_id
_ location_id
The user belongsToMany location through that table. I also have another model:
Region
- id
- name
I defined Region hasMany Locations.
With those relationships, how do I define a relationship between User and Region which Region will be able to find all users under all Locations associated with it?
<?php
class User extends Model
{
public function locations() {
return $this->belongsToMany('App\Location', 'user_location');
}
}
class Location extends Model
{
public function users() {
return $this->belongsToMany('App\User', 'user_location');
}
public function region() {
return $this->belongsTo('App\Region', 'region_id');
}
}
class Region extends Model
{
public function locations() {
return $this->hasMany('App\Location', 'region_id');
}
public function users() {
// what am I supposed to put in here?
}
}
There is no native relationship for this case.
I created a HasManyThrough relationship for situations like this: Repository on GitHub
After the installation, you can use it like this:
class Region extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function users() {
return $this->hasManyDeep(User::class, [Location::class, 'user_location']);
}
}

Laravel 5.4 table relationship between 3 small tables

I have a recently started learning Laravel 5.4, I am having trouble with my 3 way table relationship, I've looked at a few articles online regarding many to many, but this relationship is just a "hasOne" on both sides.
Could anyone give me a helpful hint as to how to structure my table relationship, here is the PK/FK relationship:
Users table (id)
Listings table (id, user_id)
Insights table (id, listing_id) - one insight row per listing only.
And the models below:
Users Model
class User extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
And what I am trying to achieve is to query the users own listings, with each listings current insights.
Thanks a bunch.
Simon.
User model
class User extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
And if you want query users with Listing and Insight
$users = User::with(['listing', 'listing.insight'])->get();
foreach($users as $user) {
$user->listing->insight;
}
class User extends Model
{
public function listing()
{
return $this->hasMany(Listing::class);
}
}
class Listing extends Model
{
public function insight()
{
return $this->hasOne(Insight::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
class Insight extends Model
{
public function listing()
{
return $this->belongsTo(Listing::class);
}
}
$users = User::with('listing.insight')->get();
foreach($users as $user) {
$user->listing->insight;
}

Laravel Eloquent Relationship Through Another Table

I have the following database tables:
Seasons
id
number
Teams
id
name
Standings
id
season_id
team_id
The question is, how could I get all of the teams in a season through the standings table. At the moment I am getting all of the teams this way:
$teams = [];
$standings = $season->standings;
foreach($standings as $standing){
$teams[] = $standing->team;
}
Is there a way I could do this using Eloquent relationships? I have tried HasManyThrough with no success. These are what my models look like currently:
class Season extends Eloquent{
public function standings(){
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent{
public function team(){
return $this->belongsTo('Team');
}
}
class Team extends Eloquent{
public function standings(){
return $this->belongsToMany('Standing');
}
}
Your relationships look a little off. Here is all the relationships you should need though only the belongsToMany ones are required for this specific scenario of finding all the teams in a season.
class Season extends Eloquent {
public function teams()
{
return $this->belongsToMany('Team', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Team extends Eloquent {
public function seasons()
{
return $this->belongsToMany('Season', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent {
public function team()
{
return $this->belongsTo('Team');
}
public function season()
{
return $this->belongsTo('Season');
}
}
You would use the belongsToMany relationship rather than a hasManyThrough to query all the teams in a season. That would look something like...
Season::with('teams')->find($season_id);
foreach($season->teams as $team) {
echo $team->name;
}

Resources