Laravel Collection 通过键获取值之一

我是 Laravel 的新手,我试图通过从另一个集合中检索到的一个 ID 来获取一个集合中的一个值。


我的函数返回 2 个集合:


public function index()

{

    $category = BlogCategory::all(['id', 'name']);        

    $post = BlogPost::orderBy('id', 'desc')->take(14)->get();

    return view('blog.index', ['posts' => $post],  ['categories' => $category]);

}

在 foreach 循环中,我从集合中获取值:


    @foreach($posts as $post)

     @if($loop->iteration > 2)

        <div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">

          <a href="#">

             <img src="#" alt="">

             <p class="post-category">{{ $categories->get($post->category_id) }}</p>

             <h5 class="post-title">{{ $post->title }}</h5>                        

          </a>

         </div>

      @endif

     @endforeach

我部分得到了如下图所示的结果,但我只想得到名称。

http://img1.mukewang.com/61a9f5fe00019c4609730032.jpg

这是我想弄清楚的代码 {{ $categories->get($post->category_id) }}


如果有更好或更正确的方法,请告诉我。


博客文章迁移:


public function up()

{

    Schema::create('blog_posts', function (Blueprint $table) {

        $table->bigIncrements('id');

        $table->string('title');

        $table->longText('content');

        $table->mediumText('slug');

        $table->bigInteger('author_id')->unsigned();

        $table->bigInteger('category_id')->unsigned();

        $table->timestamps();

        $table->foreign('author_id')->references('id')->on('blog_authors');

        $table->foreign('category_id')->references('id')->on('blog_categories');

    });

}



Qyouu
浏览 275回答 3
3回答

jeck猫

您应该建立BlogPost和BlogCategory模型之间的关系,看到您已经category_id在BlogPost模型中有一个字段,即:在BlogPost型号中:public function category(){&nbsp; &nbsp;return $this->belongsTo(\App\BlogCategory::class);}在BlogCategory型号中:public function posts(){&nbsp; &nbsp;return $this->hasMany(\App\BlogPost::class);}接下来,您可以渴望的负载 categories与$posts在你的控制器只有两个疑问:public function index(){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $posts = BlogPost::with('category')->orderBy('id', 'desc')->take(14)->get();&nbsp; &nbsp; return view('blog.index', compact('posts'));}然后在您的视图中,您可以$post->category直接访问每个对象,因为在控制器中加载了热切:@foreach($posts as $post)&nbsp;@if($loop->iteration > 2)&nbsp; &nbsp; <div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">&nbsp; &nbsp; &nbsp; <a href="#">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="#" alt="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p class="post-category">{{ $post->category->name }}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h5 class="post-title">{{ $post->title }}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp;</div>&nbsp; @endif&nbsp;@endforeach

婷婷同学_

它可以优化,首先,您需要从类别到帖子建立一对多的关系首先:确保您在帖子迁移category_id列中有第二:打开类别模型并编写此方法,这将允许您获取属于该类别的帖子public function posts(){&nbsp; &nbsp;return $this->hasMany(\App\Post::class);}第三:打开店铺模型并编写此方法,这将允许您获取属于帖子的类别public function catgeory(){&nbsp; &nbsp;return $this->belongsTo(\App\Category::class);}最后:您将像这样编辑您的视图@foreach($posts as $post)&nbsp; &nbsp; <div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">&nbsp; &nbsp; &nbsp; <a href="#">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="#" alt="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p class="post-category">{{ $post->category->title }}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<h5 class="post-title">{{ $post->title }}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp;</div>&nbsp;@endforeach当然,您不会再在控制器中调用类别public function index(){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; $post = BlogPost::orderBy('id', 'desc')->take(14)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('category')->get();&nbsp; return view('blog.index', ['posts' => $post]);}

慕容708150

此行为$categories->get($post->category_id)您返回一个数组category,因此这里的解决方案如下所示:{{&nbsp;$categories->get($post->category_id)['name']&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP