Reputation: 1546
I am using Laravel eloquent relationship, when i use
{{$subject->cat}}
i receive a json response like below
{"id":13,"name":"Fsc","created_at":"2017-10-23 00:00:00","updated_at":"2017-10-23 00:00:00"}
as i have 2nd object "name" here i tried
{{$subject->cat->name}}
but got error
Trying to get property of non-object
while i am using same approach for other table and copied same method here but getting error.
See my Blade file code, i am calling object inside foreach loop
@foreach ($subjects as $subject)
<tr>
<td width="8%">{{$subject->id}}</td>
<td width="22%">{{$subject->subject}} </td>
<td width="22%">{{$subject->cat->name}}</td>
<!-- <a class="btn btn-success btn-sm" href="{{route('subjects.show', $subject->id)}}"><i class="fa fa-eye"></i></a> | -->
<a href="{{route('subjects.edit', $subject->id)}}" class="btn btn-warning btn-sm"><i class="fa fa-pencil"></i></a> |
{!! Form::open(['route' => ['subjects.destroy', $subject->id], 'method' => 'DELETE', 'class' => 'delete-form']) !!}
{{ Form::button('<i class="fa fa-times" aria-hidden="true"></i>', ['class' => 'btn btn-danger btn-sm pull-left', 'type' => 'submit']) }}
{!! Form::close() !!}
</td>
</tr>
@endforeach
Upvotes: 3
Views: 1555
Reputation: 2372
Try this
$cat = json_decode($subject->cat); // returns object
{{ $cat['name'] }}
or
$cat = json_decode($subject->cat, true); // returns array
{{ $cat['name'] }}
Upvotes: 1
Reputation: 3935
You need to decode your json and then use like this:
$result = json_decode ($subject->cat);
echo $result->name;
Use your variable name in place of $result
For insight here is pastebin demo
Upvotes: 1
Reputation: 5042
Just use json_decode
like:
$var = json_decode($subject, true);
Then you can use:
{{$subject->cat->name}}
See more here!
Hope this helps you!
Upvotes: 1
Reputation: 4547
Takes a JSON encoded string and converts it into a PHP variable.
Then
Use {{$subject->cat}}
Upvotes: 2