Php Laravel集合按键获取其中一个值

Php Laravel集合按键获取其中一个值,php,laravel-5,Php,Laravel 5,我是Laravel的新手,我正在尝试通过从另一个集合检索的ID获取一个集合中的一个值 My函数返回2个集合: public function index() { $category = BlogCategory::all(['id', 'name']); $post = BlogPost::orderBy('id', 'desc')->take(14)->get(); return view('blog.index', ['posts' =&g

我是Laravel的新手,我正在尝试通过从另一个集合检索的ID获取一个集合中的一个值

My函数返回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($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
在foreach循环中,我从集合中获取值:

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($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

这一行
$categories->get($post->categority\u id)
为您返回一个
category
数组,因此这里为您提供的解决方案如下所示:

{{ $categories->get($post->category_id)['name'] }}

它可以被优化,首先,你需要建立从类别到帖子的一对多关系

首先:确保在POST迁移
类别id
列中

Second:打开类别模型并编写此方法这将允许您获取属于该类别的帖子

public function posts(){
   return $this->hasMany(\App\Post::class);
}
Third:打开店铺模型并编写此方法这将允许您获取属于该帖子的类别

public function catgeory(){
   return $this->belongsTo(\App\Category::class);
}
最后:您将像这样编辑视图

@foreach($posts as $post)
    <div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
      <a href="#">
         <img src="#" alt="">
         <p class="post-category">{{ $post->category->title }}</p>
         <h5 class="post-title">{{ $post->title }}</h5>                        
      </a>
     </div>
 @endforeach

也许最好用关系来推动这种局面。您可以通过这种方式在控制器中加载数据

在范文中:

function categories(){
return $this->belongsToMany('App\BlogCategory')->select(array('id', 'name');
}
如果没有表轴,可能是hasOne->关系

在控制器中:

    public function index()
{     
    $data['posts'] = BlogPost::orderBy('id', 'desc')->take(14)->with('categories')->get();
    return view('blog.index', $data);
}
他认为:

@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">{{ $post->categories->name }}</p>
             <h5 class="post-title">{{ $post->title }}</h5>                        
          </a>
         </div>
      @endif
     @endforeach
@foreach($posts as$post)
@如果($loop->iteration>2)
@恩迪夫
@endforeach

您应该建立
BlogPost
blogcontegory
模型之间的关系,因为您在
BlogPost
模型中已经有了
category\u id
字段,即:

BlogPost
模型中:

公共功能类别(){
返回$this->belongsTo(\App\blogcontegory::class);
}
博客类别中
模型:

公共职能岗位(){
返回$this->hasMany(\App\BlogPost::class);
}
接下来,您可以在控制器中使用
$posts
进行
分类
,只需两个查询:

公共功能索引()
{        
$posts=BlogPost::with('category')->orderBy('id','desc')->take(14)->get();
返回视图('blog.index',压缩('posts');
}
然后在您的视图中,您可以直接访问每个
$post->category
对象,因为控制器中加载了:

    public function index()
{     
    $data['posts'] = BlogPost::orderBy('id', 'desc')->take(14)->with('categories')->get();
    return view('blog.index', $data);
}
@foreach($posts as$post)
@如果($loop->iteration>2)
@恩迪夫
@endforeach


它给出了一个错误:
尝试获取非对象的属性“name”
尝试获取非对象的属性“name”
@AlanGodoidaSilveira实际上,由于get方法返回的值是一个简单数组,现在请检查顺序是什么意思?你每行有多少个名字?你忘了用BlogPost加载类别,你的控制器/视图代码将发送15个查询,而不是只有两个。很好的一个我忘了,我现在就修复它。感谢你注意到我在模型[App\BlogPost]上调用未定义的关系[categories]是的,它成功了。但在你回答之前,我试过另一个正确答案。当然。接受的答案也在使用即时加载。使用belongToMany:
基表或未找到视图:1146表“eaglehorn.blog\u category\u blog\u post”不存在(SQL:select
id
name
blog\u category\u blog\u post`With hasOne:
Column not found:1054未知列'blog\u categories.blog\u post\u id'
您需要添加与这两个表相关的列。如果字段相关的是categority\u id:return$this->hasOne('App\blogcontegority','category\u id')->选择(数组('id','name'));关系加载表之间的相关信息。检查以下位置的信息:如果关系是一对一:如果每个帖子都有一些类别:一对多
“未定义变量:类别”
我的代码中没有任何$categories变量,您应该在错误的答案中发布,请尝试我的代码。当然。重新启动服务器需要它。它可以根据需要工作。使用快速加载而不是延迟加载将使(n+2)SQL Opartition控制器正在快速加载:
BlogPost::with('category'))
这是两个问题,请阅读此处: