Search code examples
phplaraveleloquentblogs

How can I retrieve the name of the categories to which a post belongs


I'm learning Laravel by developing a small blog in order to learn the framework and what it has to offer. The problem I'm now facing is that "How can I retrive the name of the categories to which the post originally belong to"

My relationship between posts and categories is many to many. I have three Tables

Post
Category
CategoryPost

My Post Model

public function categories()
    {
        return $this->belongsToMany('App\category');
    }

My Categories Model

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

Other than that I have not made any model of the pivot table. Do I need to make it?

The records present in my pivot table are as follows:

    id  category_id  post_id  
------  -----------  ---------
     6            1         16
     7            2         16
     8            3         16
     9            1         17
    10            3         17
    11            1         18
    12            2         18

I want to display the name of the current category to which a post belongs to originally so that the user can add or remove the categories in the edit page.

These are my migrations:

Post Table

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->Integer('status_id');
            $table->string('title');
            $table->text('body');
            $table->text('media');
            $table->string('tags');
            $table->timestamps();
        });
    }

Category Table:

 public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->text('description');
        $table->timestamps();
    });
}

CategoryPost Table:

 public function up()
{
    Schema::create('category_post', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_id');
        $table->integer('post_id');
        $table->timestamps();
    });
}

Solution

  • Edit migration:

    CategoryPost Table

    public function up()
    {
        Schema::create('category_post', function (Blueprint $table) {
            $table->increments('id');
                $table->Integer('category_id')->unsigned();
                $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
                $table->Integer('post_id')->unsigned();
                $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
                $table->timestamps();
        });
    }
    

    Then in your controller:

    public function edit(Post $post) {
     $post=Post::with('categories')->find($post->id);
     $categories=Category::all(); 
    
     return view('admin.pages.post.edit',compact('post','categories')); }
    

    And in your blade you are be able to do something like this:

    @foreach($post->categories as $category)
        {{ $category->name }}
    @endforeach
    

    This should works, but if you have errors provide it here.