This is the second blog post of Laravel relationship Model tutorial series. In this tutorial, we are going to learn how to implement One to Many relationship within our Laravel application

Prerequisites

  • Laravel
  • To Continue with this tutorial, you should have installed Laravel in your pc. If you are still not installed Laravel in your machine you can configure it from here.

    Setting up the Project

    First you need to create a new laravel project by running below command in your terminal
    1
    composer create-project laravel/one-to-many-example

    Then navigate to your project directory by using below command in your terminal

    1
    cd one-to-many-example

    One to Many Relationship will use “hasMany()” and “belongsTo()” for relation.

    Create Migration

    Now we have to create migration of "posts" and "comments" table and also add foreign key with posts table.
  • Posts table migration
  • 1
    2
    3
    4
    5
    Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->string("name");
    $table->timestamps();
    });
  • Comments table migration
  • 1
    2
    3
    4
    5
    6
    7
    8
    Schema::create('comments', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('post_id')->unsigned();
    $table->string("comment");
    $table->timestamps();
    $table->foreign('post_id')->references('id')->on('posts')
    ->onDelete('cascade');
    });

    Create Models

    Now we need to create User and Book table model. we will also use "hasMany()" and "belongsTo()" for relationship of both model.
  • Post Model:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?php

    namespace App;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;


    class Post extends Model
    {
    public function comments()
    {
    return $this->hasMany(Comment::class);
    }
    }
  • Comment Model
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;

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

    Retrieve Records

    1
    2
    3
    4
    5
    6
    7
    $post = Post::find(1);
    $comments = $post->comments;
    dd($comments);

    $comment = Comment::find(1);
    $post = $comment->post;
    dd($post);

    Create Records

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $post = Post::find(1);
    $comment = new Comment;
    $comment->comment = "Welcome to CodingtrickS";
    $post = $post->comments()->save($comment);

    $post = Post::find(1);
    $comment1 = new Comment;
    $comment1->comment = "Welcome to CodingtrickS";
    $comment2 = new Comment;
    $comment2->comment = "Welcome to CodingtrickS";
    $post = $post->comments()->saveMany([$comment1, $comment2]);

    $comment = Comment::find(1);
    $post = Post::find(2);
    $comment->post()->associate($post)->save();

    In this tutorial, we learn how to implement One to Many relationship within Laravel. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail.