duan4739 2016-03-17 01:49
浏览 37
已采纳

Laravel在视图中访问阵列数据

I know I am very close, I'm just stuck and can't figure out this last step.

I'm trying to echo out some information in my blade template, but I'm getting the following error: Undefined property: Illuminate\Database\Eloquent\Collection::$Moniker

Here is my controller, which returns the $data variable to the view:

$user = User::where('id', $request)->get();
$data = array(
    'images'  => $images,
    'user'   => $user
);
return view('a_profile')->with('data', $data);  

Here is my view:

<title>{{$data['user']->Moniker}} | My Company | Baltimore, MD</title>

How do I get the Moniker which is a column in my users table to echo out, in this case? Thank you! :)

  • 写回答

1条回答 默认 最新

  • dongzi5062 2016-03-17 03:31
    关注

    using get() method will return you records in array into array format like this,

    your query,

    $user = User::where('id', $request)->get();
    

    result would be this

    array(
       array(
         'id'  =>1,
         'name'=>'xyz' 
       )
    )
    

    So, in your view, instead of looping, you can access the column doing this,

    <title>{{$data['user'][0]->Moniker}} | My Company | Baltimore, MD</title>
    

    So convert your query into this first(), because you want to return single record,

    $user = User::where('id', $request)->first();
    

    this will return the result into single array format

    array(
      'id'  =>1,
      'name'=>'xyz' 
    )
    

    and you can access into your view, simple doing this

    <title>{{$data['user']->Moniker}} | My Company | Baltimore, MD</title>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失