Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel hasMany and belongsTo parameters

Tags:

I have a table store, and store has many libraries, in library I have foreign key of store store_id.

Store table

id(PK) 

Library table

id(PK) store_id(FK) 

I'm confused with hasMany and belongsTo parameters include, in the docs it says

return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

return $this->belongsTo('App\Post', 'foreign_key', 'other_key');

Which table of hasMany foreign_key and local_key came from? Same with belongsTo which table of foreign_key and other_key came from?

Store model

public function library(){     return $this->hasMany('App\Library', 'what_foreign_key_should_be_here','what_other_key_should_be_here'); } 

Library model

public function stores(){     return $this->belongsTo('App\Stores', 'what_foreign_key_should_be_here', 'what_other_key_should_be_here'); } 

Because sometimes I change my primary key id of a table to other name like sid, so I always want to specify which is foreign key and primary key

like image 964
Naib Sorion Avatar asked Oct 20 '17 01:10

Naib Sorion


People also ask

What is the difference between hasOne and belongsTo?

The only difference between hasOne and belongsTo is where the foreign key column is located. Let's say you have two entities: User and an Account. In short hasOne and belongsTo are inverses of one another - if one record belongTo the other, the other hasOne of the first.

What is hasMany in laravel?

hasMany relationship in laravel is used to create the relation between two tables. hasMany means create the relation one to Many. For example if a article have comments and we wanted to get all comments of the article then we can use hasMany relationship .

How do you make a hasMany relationship in laravel?

Step 1: Download Laravel App. Step 2: Connect App to Database. Step 3: Create Model And Migration. Step 4: Set One To Many Relationship.

How do you call a hasMany in laravel?

The hasMany() function returns an Illuminate\Database\Eloquent\Relations\HasMany object. So, when you call $user->websites() , this will return the HasMany relationship object. Wherever you are using this function, you are then trying to convert that result to a string, which is the error you're seeing.


Video Answer


1 Answers

To simplify the syntax, think of the return $this->hasMany('App\Comment', 'foreign_key', 'local_key'); parameters as:

  1. The model you want to link to
  2. The column of the foreign table (the table you are linking to) that links back to the id column of the current table (unless you are specifying the third parameter, in which case it will use that)
  3. The column of the current table that should be used - i.e if you don't want the foreign key of the other table to link to the id column of the current table

In your circumstance, because you have used store_id in the libraries table, you've made life easy for yourself. The below should work perfectly when defined in your Store model:

public function libraries() {     return $this->hasMany('App\Library'); } 

Behind the scenes, Laravel will automatically link the id column of the Store table to the store_id column of the Library table.

If you wanted to explicitly define it, then you would do it like this:

public function libraries(){     return $this->hasMany('App\Library', 'store_id','id'); } 
  • A model standard is that singularly-named functions return a belongsTo, while a plural function returns a hasMany (ie. $store->libraries() or $library->store()).
like image 119
James Avatar answered Sep 23 '22 16:09

James