为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
翻翻过去那场雪 2023-06-24 18:32:48
在我的 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'));        }    }    我的帖子模型<?phpnamespace 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 文件呈现的帖子标题
查看完整描述

2 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

所以看起来您正在根据您想要显示的帖子通过以下关系加载所有帖子$post->belongsTo(Category::class)(这就是我想象的帖子模型的样子)


我要做的就是这个。


在我的控制器中:

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

    }

}


查看完整回答
反对 回复 2023-06-24
?
蓝山帝景

TA贡献1843条经验 获得超7个赞

下面的方法通过在循环中添加 except 方法来解决该问题。


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

         @foreach($category->posts->except($post->id) as $catPost)

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

         <br>

         @endforeach

     @endforeach 

查看完整回答
反对 回复 2023-06-24
  • 2 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号