donglao7947 2015-09-14 05:11
浏览 57
已采纳

试图在视图中获取非对象的属性?

i have follwing data in my array

Array
(
    [id] => 11
    [username] => username
    [avatar] => uploads/5.jpg
    [email] => username@example.com
    [designation] => TT
    [supervisor] => Array
        (
            [id] => 3
            [username] => alex
            [avatar] => uploads/3.jpg
            [email] => alex@example.com
            [designation] => ADM
            [supervisor] => 
            [created_at] => 2015-09-10 03:58:46
            [updated_at] => 2015-09-10 03:58:46
        )

    [created_at] => 2015-09-10 04:29:47
    [updated_at] => 2015-09-10 04:29:47
)

i am trying to access supervisor username but it always throw an error like

Trying to get property of non-object in view

i have tried to access that in following method but it doesn't work

$single_view_user->supervisor->username


$single_view_user['supervisor']->username

 $single_view_user['supervisor']['username']

can any one tell what i am doing wrong ? thank you

update

in my view for printing purpose i am using

{{print_r($single_view_user->toArray())}}

 <tr class="active">
             <th>Profile Picture</th>
                <td><img src="{{asset($single_view_user->avatar)}}" ></td>
            </tr>
            <tr class="active">
             <th>Username </th>
                <td>{{$single_view_user->username}}</td>
            </tr>

           <tr class="active">
             <th>email</th>
                <td>{{$single_view_user->email}}</td>
            </tr>
             <tr class="active">
             <th >Designation</th>
                <td>{{$single_view_user->designation}}</td>
            </tr>
             <tr class="active">
             <th>supervisor</th>
             <?php $single_view_user['supervisor']['username']; ?>
                <td>{{ $single_view_user['supervisor']->username}}</td>
            </tr>

and in my controller

$single_view_user = User::where('id',$id)->with('Supervisor')->first();

        return view('admin/single_view_user',['single_view_user'=>$single_view_user]);

Update 2

@foreach($single_view_user as $val)


{{print_r($val) }}

@endforeach

ouput will be like

11 11 11 1 
  • 写回答

3条回答 默认 最新

  • dqt66847 2015-09-14 05:42
    关注

    Based on your with() method, it looks like your relationship method is Supervisor, not supervisor (uppercase vs lowercase). When you call toArray on the Model, it runs the relationship attributes through the Str::snake() method, which also lowercases them, which is why Supervisor appears as lowercase in your array output.

    Try:

    $single_view_user = User::where('id', $id)->with('Supervisor')->first();
    dd($single_view_user->Supervisor->username);
    

    As a side note, you could clean up your query a little bit as:

    $single_view_user = User::with('Supervisor')->find($id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘