Header Ads

Header ADS

Laravel Eloquent Relationships

 Laravel-er Eloquent relationships hocche model-gulor moddhe relation establish korar ekta powerful system. Ekhane ami One to One theke shuru kore Polymorphic porjonto shob gulo relationship Banglay explain korbo, example code shoho. Simple & complex duita rokomer example-i dibo.


 1. One to One Relationship (একজন user এর একটি profile)

Simple Example:

User Model:


public function profile() { return $this->hasOne(Profile::class); }

Profile Model:


public function user() { return $this->belongsTo(User::class); }

Use Example:


$user = User::find(1); echo $user->profile->phone;

 2. One to Many Relationship (একটা post-এর একাধিক comment)

 Simple Example:

Post Model:


public function comments() { return $this->hasMany(Comment::class); }

Comment Model:


public function post() { return $this->belongsTo(Post::class); }

Use Example:


$post = Post::find(1); foreach ($post->comments as $comment) { echo $comment->content; }

 3. Many to Many Relationship (একজন student একাধিক course-e porche, r ekta course-e onek student ache)

Simple Example:

Student Model:


public function courses() { return $this->belongsToMany(Course::class); }

Course Model:


public function students() { return $this->belongsToMany(Student::class); }

Pivot Table: course_student with student_id, course_id

Use Example:


$student = Student::find(1); foreach ($student->courses as $course) { echo $course->name; }

 4. Has Many Through (ekta country-r onek user ache, user-r onek post ache — country diye post ber kora)

 Complex Example:

Country Model:


public function posts() { return $this->hasManyThrough(Post::class, User::class); }

Use Example:


$country = Country::find(1); foreach ($country->posts as $post) { echo $post->title; }

 5. Polymorphic Relationship (ekoi table er record onek model theke ashte pare, e.g. comments for posts, videos)

Simple Polymorphic (comment on post or video)

Migration:


$table->text('body'); $table->morphs('commentable'); // commentable_id, commentable_type

Comment Model:


public function commentable() { return $this->morphTo(); }

Post Model:


public function comments() { return $this->morphMany(Comment::class, 'commentable'); }

Video Model:


public function comments() { return $this->morphMany(Comment::class, 'commentable'); }

Use Example:


$post = Post::find(1); $post->comments()->create(['body' => 'Great post!']); $video = Video::find(1); $video->comments()->create(['body' => 'Awesome video!']);

 6. Polymorphic Many to Many (Tagging system: Post & Video both can have Tags)

 Complex Polymorphic Example:

Tables:

  • tags

  • taggables (tag_id, taggable_id, taggable_type)

Tag Model:


public function posts() { return $this->morphedByMany(Post::class, 'taggable'); } public function videos() { return $this->morphedByMany(Video::class, 'taggable'); }

Post & Video Models:


public function tags() { return $this->morphToMany(Tag::class, 'taggable'); }

Use Example:


$tag = Tag::find(1); foreach ($tag->posts as $post) { echo $post->title; }




Bonus: Load Relationships with Eager Loading


$posts = Post::with('comments')->get();

 Summary Table (Banglay)   



Relationship TypeDescription (Banglay)
One to One১টা user এর ১টা profile
One to Many১টা post এর অনেক comment
Many to Many১টা student onek course e, ১ta course onek student e
Has Many ThroughCountry → Users → Posts
Polymorphic One to ManyPost o Video er upor comment
Polymorphic Many to ManyPost o Video er upor tag

No comments

Theme images by fpm. Powered by Blogger.