Reputation: 8078
i have follwing data in my array
Array
(
[id] => 11
[username] => username
[avatar] => uploads/5.jpg
[email] => [email protected]
[designation] => TT
[supervisor] => Array
(
[id] => 3
[username] => alex
[avatar] => uploads/3.jpg
[email] => [email protected]
[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
Upvotes: 0
Views: 348
Reputation: 3407
It should be:
$single_view_user['supervisor']['username'];
Because inside array is array not an object.
In your controller;
$single_view_user = $single_view_user = User::where('id',$id)->with('Supervisor')->first();
$convert = $single_view_user->toArray();
return view('admin/single_view_user',compact('convert',$convert));
In your view:
<tr class="active">
<th>Profile Picture</th>
<td><img src="{{asset($convert['avatar'])}}" ></td>
</tr>
<tr class="active">
<th>Username </th>
<td>{{$convert['username']}}</td>
</tr>
<tr class="active">
<th>email</th>
<td>{{$convert['email']}}</td>
</tr>
<tr class="active">
<th >Designation</th>
<td>{{$convert['designation']}}</td>
</tr>
<tr class="active">
<th>supervisor</th>
{{$convert['supervisor']['username']}}
<td>{{$convert['supervisor']['username']}}</td>
</tr>
Upvotes: 1
Reputation: 62228
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);
Upvotes: 1
Reputation: 2807
use the below code:
echo $yourarray['supervisor']['username'];
->
operator is used to access the property inside an object but what you have is an array. Array are accessed using the index.
Edited Part Answer: In your view use the below code:
<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>
Upvotes: 1