1
votes

In my blade.php file that is displaying a single post is working perfectly well. In the blade I also have the access of the categories associated with the post i.e $post->categories and is working too.

The single post that I'm displaying from the .blade.php with a title in the header has got only one category and that is football.

When I loop over the posts title associated with the category of the post being displayed from the .blade.php file using the code below it works well and I get the titles of the posts associated with the category football as expected!

 @foreach($post->categories as $category)
        @foreach($category->posts as $post)
            <p>{{ $post->title }}</p>
                <br>
        @endforeach
    @endforeach

MY CHALLENGE: I'd like to exclude the title of the post that is also the title of the post of being displayed so that I don't repeat a title at the bottom of my page that was already in the header of the page.

MY CONTROLLER!

    <?php
    
    namespace App\Http\Controllers\User;
    
    use App\Http\Controllers\Controller;
    use App\Model\user\post;
    
    use Illuminate\Http\Request;
    
    class PostController extends Controller
    {
        public function post(post $post)
        {
    
            return view('user.post', compact('post'));
        }
    }

    

MY Post Model

<?php

namespace App\Model\user;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function tags()
    {
        return $this->belongsToMany('App\Model\user\Tag', 'post_tags')->withTimestamps();
    }

    public function categories()
    {
        return $this->belongsToMany('App\Model\user\Category', 'category_posts');
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }
}

My Category Model


   

     <?php
        
        namespace App\Model\user;
        
        use Illuminate\Database\Eloquent\Model;
        
        class Category extends Model
        {
            public function posts()
            {
                return $this->belongsToMany('App\Model\user\post', 'category_posts');
        
            }
        
            public function getRouteKeyName()
            {
                return 'slug';
            }
        }

my Category_post Model


    <?php
    
    namespace App\Model\user;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Category_post extends Model
    {
        public function posts()
        {
            return $this->belongsToMany('App\Model\user\Post', 'category_posts');
        }
    
    }

Please note that everything is working perfectly fine. When I loop over $category->posts title in my .blade.php file I get the posts title as expected. Now I just want to exclude the title of the post that is rendered by the blade.php file

2
Can you please show the controller logicFunkyMonk91
@FunkyMonk91 I've edited and added the controller to the post!Alphy Gacheru
Interesting. I think the problem stems a bit deeper. Could you show the relationship in your Post model to the categories? Thanks.FunkyMonk91
@ FunkyMonk91 I have added that in the post! please note that everything is working perfectly fine. When I loop over $category->posts title in my .blade.php file I get the posts title as expected. Now I just want to exclude the title of the post that is rendered by the blade.php fileAlphy Gacheru

2 Answers

1
votes

So it looks like you are loading all the posts based off the one you want to show, via the relationship of $post->belongsTo(Category::class) (this is what I imagine your Post model looks like)

What I would do is this.

In my controller:

class PostController extends Controller
{
    public function post(post $post)
    {
        // eager load the categories and their posts
        $post->load([
            'categories',
            // need to pass the selected post into the scope of the callback
            'categories.posts' => function ($query) use ($post) {
                // give us all the posts, besides this one, that belong to the category
                $query->where('id', '!=', $post->id)
            }
        ]);

        return view('user.post', compact('post'));
    }
}
1
votes

The following approach solves the problem by adding the except method in the loop. You can also check out @FunkyMonk91 approach in the comments which also solves the problem by achieving the same objective in the controller!

  @foreach($post->categories as $category)
         @foreach($category->posts->except($post->id) as $catPost)
         <p>{{ $catPost->title }}</p>
         <br>
         @endforeach
     @endforeach 

https://laravel.com/docs/7.x/eloquent-collections?fbclid=IwAR1mnWJX5eCtXRbNnac7nAFUtR_Onk35Gqf7GRBweoz88iBQqZRGo65A-jg