猿问

在我的 show.blade.php 文件中,当我循环 $category->posts

在我的 Blade.php 文件中,显示单个帖子的效果非常好。在刀片中,我还可以访问与帖子相关的类别$post->categories,并且也正在工作。


我从 .blade.php 显示的单个帖子(标题中带有标题)只有一个类别,那就是足球。


当我使用下面的代码遍历与 .blade.php 文件中显示的帖子类别关联的帖子标题时,效果很好,并且我按预期获得了与足球类别关联的帖子标题!


 @foreach($post->categories as $category)

        @foreach($category->posts as $post)

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

                <br>

        @endforeach

    @endforeach

我的挑战:我想排除帖子的标题,这也是正在显示的帖子的标题,这样我就不会在页面底部重复已经在页面标题中的标题。


我的控制器!


    <?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'));

        }

    }


    

我的帖子模型


<?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';

    }

}

我的类别模型



   


     <?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';

            }

        }

请注意,一切都工作得很好。当我在 .blade.php 文件中循环 $category->posts 标题时,我得到了预期的帖子标题。现在我只想排除由blade.php 文件呈现的帖子标题


翻翻过去那场雪
浏览 119回答 2
2回答

忽然笑

所以看起来您正在根据您想要显示的帖子通过以下关系加载所有帖子$post->belongsTo(Category::class)(这就是我想象的帖子模型的样子)我要做的就是这个。在我的控制器中:class PostController extends Controller{&nbsp; &nbsp; public function post(post $post)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // eager load the categories and their posts&nbsp; &nbsp; &nbsp; &nbsp; $post->load([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'categories',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // need to pass the selected post into the scope of the callback&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'categories.posts' => function ($query) use ($post) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // give us all the posts, besides this one, that belong to the category&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->where('id', '!=', $post->id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; return view('user.post', compact('post'));&nbsp; &nbsp; }}

蓝山帝景

下面的方法通过在循环中添加 except 方法来解决该问题。  @foreach($post->categories as $category)         @foreach($category->posts->except($post->id) as $catPost)         <p>{{ $catPost->title }}</p>         <br>         @endforeach     @endforeach 
随时随地看视频慕课网APP
我要回答