In laravel you can define all kind of relationships between database tables on the model itself (as in laravel, model is representation of database table), so here's list of eloquent relationships that you can use in laravel model.
One to One
For one to one relationship, you can use hasOne and belongsTo.
return $this->hasOne('model_name');
return $this->hasOne('model_name', 'foreign_key');
return $this->hasOne('model_name', 'foreign_key', 'local_key');
return $this->belongsTo('model_name');
return $this->belongsTo('model_name', 'local_key');
return $this->belongsTo('model_name', 'local_key', 'parent_key');
One to ManyFor one to many relationship, you can use hasMany and belongsTo.
return $this->hasMany('model_name');
return $this->hasMany('model_name', 'foreign_key');
return $this->hasMany('model_name', 'foreign_key', 'local_key');
return $this->belongsTo('model_name');
return $this->belongsTo('model_name', 'local_key');
return $this->belongsTo('model_name', 'local_key', 'parent_key');
Many to Many
For many to many relationship, you can use belongsToMany.
return $this->belongsToMany('model_name');
return $this->belongsToMany('model_name', 'pivot_table_name');
return $this->belongsToMany('model_name', 'pivot_table_name', 'id1', 'id2');
Has many through
The has many through is similar to hasMany but the relation is involving third table.
return $this->hasManyThrough('Post', 'User');
return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
Polymorphic relations
Polymorphic relations allow a model to belong to more than one other model, on a single association. You can use morphTo and morphMany.
class Photo extends Eloquent {
public function imageable()
{
return $this->morphTo();
}
}
class Staff extends Eloquent {
public function photos()
{
return $this->morphMany('Photo', 'imageable');
}
}
class Order extends Eloquent {
public function photos()
{
return $this->morphMany('Photo', 'imageable');
}
}
Many to many polymorphic relation
In addition to traditional polymorphic relations, you may also specify many-to-many polymorphic relations. You can use morphToMany and morphedByMany.
class Post extends Eloquent {
public function tags()
{
return $this->morphToMany('Tag', 'taggable');
}
}
class Tag extends Eloquent {
public function posts()
{
return $this->morphedByMany('Post', 'taggable');
}
public function videos()
{
return $this->morphedByMany('Video', 'taggable');
}
}
All of these relationships works on laravel 4 and laravel 5
15 comments
sundul77.com Situs Agen Bola Terbaik | Judi Casino Online | poker uang asli | Bandar Slot Terpercaya
sundul77.com Adalah Situs Agen Bola Terbaik | Judi Casino Online | poker uang asli | Bandar Slot Terpercaya, Game Slot Mesin, Agen Sbobet, Agen Ibcbet, Agen Mansion88 sundul77 Merupakan Salah Satu Bandar Bola, Bandar Casino, Poker Online Terpercaya IDNSPORT. Kelebihan Bandar Bola Terbesar www.sundul77.com Desain Website Menarik, Live Casino Online 24 Jam Non-Stop Bersama Dealer Eropa & Dealer Asia..
Situs Agen Bola Terbaik | Judi Casino Online | poker uang asli | Bandar Slot Terpercaya, Game Slot Mesin, Agen Sbobet, Agen Ibcbet, Agen Mansion88
Bolagaming mempunyai tim berpengalaman dalam melayani setiap member yang bergabung di situs judi taruhan bola terbaik ini. Kami menyediakan customer service online 24 jam yang akan menemani anda dan membantu memberikan arahan kepada anda agar mudah saat melakukan pendaftaran. Anda bisa memilih jenis permainan judi taruhan online apa saja sesuai keinginan anda.
Ayo Bergabung Bersama Situs Judi Taruan Bola Terlengkap Bolagaming
situs agen bola terbaik,judi casino online,poker uang asli,poker uang asli,agen ibcbet
hello ! kiran sadvilkar visit please give me an idea can i buy this gig from fiverr.com Laravel
https://www.fiverr.com/ahmedwali5990/be-your-laravel-website-expert
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying scr888 casino game free download every little bit of it and I have you bookmarked to check out new stuff you post.
Thanks for sharing your 918kiss login thoughts with us.. they are really interesting.. I would like to read more from you.
Thanks for taking the time to discuss that, I really feel strongly about it and love learning more on that scr888 kiss918 apk download topic. If achievable, as you gain competence, would you mind updating your blog with more information? It is highly helpful for me.
Valuable 918KISS APK DOWNLOAD 2019 site, where did u come up with the information Kiss918 Download Apk in this posting? I am pleased I discovered it 918kiss company Malaysia though, ill be checking back soon to find out what new content pieces u have.
I really enjoy reading your blog as the postings mega888 download apk android
are so simple to read and follow. Outstanding. Please keep it up. Thanks.
Yeah currently I am working in Laravel Development Company and this information will help me to understand the most importent things about all kind of relationships between database tables and Laravel model.
Learn Quran Online
The online Quran Teaching Academy
Online Quran Tutor
You're the best to share us about this redesign. Trust you won't get tired on making posts as enlightening as this. Kiss918 apk download
Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. situs judi online
I have a hard time describing my thoughts on content, but I really felt I should here.Your article is really great.I like the way you wrote this information. Express mail
Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next..
파워볼사이트
EmoticonEmoticon