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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭