繁体   English   中英

如何从数组中仅选择一个选项?

[英]How to select only one option from an array?

我需要知道如何从数组中选择一个选项,我需要为两个图标创建一个@if ,但是鉴于我收到了更多我需要的信息。 控制器:

  $categoryID =  \App\Category::pluck('id');

            if($filter == 'all' || $filter == 'news')
            {
                $news = \App\News::with('user','category')->whereHas('user', function($query) {
                        $query->where('deleted_at', '=', null);
                    })->whereIn('category_id',$categoryID)->where(function($query1) use ($id) { $query1->where('public','=', 1)->orWhereHas('user.contact', function ($query2) use ($id) {
                            $query2->where('contacts.user_id', '=', $id);
                            })->orWhere('user_id','=',$id);
                        });


$temp['roleNews'] = $news->user->role;

查看代码:

{{$noticeboard_data['roleNews']}}

我收到的是:

[

 {"id":1,
 "slug":"individuals",
  "name":"Individuals",
  "permissions":"",
  "created_at":null,
  "updated_at":null,
  "pivot":{
    "user_id":"542",
    "role_id":"1"}
  }
] 

我只需要接收每个用户的role_id ,因为我将使用它来进行如下操作:

@if($noticeboard_data['roleNews'] == 1)
    <i style="font-size: 15px" class="icon-user"></i> 
                        @elseif($noticeboard_data['roleNews'] == 100)
    <i style="font-size: 15px" class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
@endif

也许我误解了这个问题,但是为什么不使用如下所示的关键路径呢:

$noticeboard_data['roleNews']['pivot']['role_id']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM