The complete Lavarel framework development tutorial

Powered by Blogger.

Thursday 2 March 2017

Laravel hasMany() and belongsTo() Relationship Tutorial


The database tables are related to each other, so that we can fetch data in a meaningful way. For example an order table should be related to customer table, so that we can know who has placed the order. The relationship can be of many types like one-to-one, one-to-many etc. The laravel hasMany() and belongsTo() relationship is basically one-to-many relationship and its reverse.

In Laravel we can define relationship by just placing a function on the laravel model. So let's discuss more about Laravel hasMany() and belongsTo() Relationship.

Laravel hasMany() and belongsTo() Relationship: Introduction

Take an example of blog post. A blog post can have many comments, so it is a one to many relationship. And its reverse all the comments belongs to a blog post. Laravel eloquent provides some methods that can be used to easily relate database tables. In this tutorial, we are going to learn about two methods that is hasMany() and belongsTo().

Laravel hasMany() and belongsTo() Relationship: Database Tables

Let's have an example of blog post and comments. So, we need to create two tables posts and comments.

Table structure

Table Name Columns
posts id, title, body
comments id, post_id, body
The post_id column will be used as a foreign key to relate comment with the post. You don't need to add foreign key attribute to the column. Laravel will take care of all this.

Create the corresponding model and insert some dummy data too.

To learn about laravel model Click Here.

Laravel hasMany() and belongsTo() Relationship: Defining Relationship

As we have already discussed above, we need to write some method to define relationship between database tables. Here a post can have multiple comments, so we can define a method on Post model-

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

Similarly, in Comment model we can define a method-

public function post(){
 return $this->belongsTo('App\Post');
}

Laravel hasMany() and belongsTo() Relationship: Accessing Collection

Now we can access all the comments of a post whose id is 1-

$comments = Post::find(1)->comments;

Printing collection to Laravel view

You may want to display all of your comments below to a blog post. Here is an example of how you can display your post and all the related comments to a page.

First you need to fetch the post. For this you can use the find() method in your controller.
To learn about Laravel Controllers Click Here.

public function show($id){
 $post = Post::find($id);
 return view('your-view-name', compact('post'));
}

Now, print the post title and body to your view.

<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>

and to print all the comments of the above post you can use foreach loop like this-

@foreach($post->comments as $comment)
 <li>{{$comment->body}} </li>
@endforeach

You can also find the parent of a comment. Use below code to find out, which post the comment belongs to.

$comment->post->title

Conclusion

This is how we can define relationships between database tables and can also access related data. Laravel also provides some other type of relations that you will learn in different tutorials. If you learned about Laravel hasMany() and belongsTo() Relationship then share to help others too.

1 comment: