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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题