I installed a Laravel, Now I want use from a site api and see its services in my site, but when I open "API SERVICES PAGE", this error is appeared:
"Trying to get property 'service' of non-object (View: /home/core/resources/views/admin/apiServices.blade.php)"
for this line:
<td>{{ isset($item->service->id) ? $item->service->id : $item->service}}</td>
My page codes:
<div class="row">
<div class="col-md-12">
<div class="tile">
<h3 class="tile-title">API Services List</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Service ID</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Min</th>
<th>Max</th>
</tr>
</thead>
<tbody>
@foreach($items as $key => $item)
<tr>
<td>{{ (int) $key+1 }}</td>
<td>{{ isset($item->service->id) ? $item->service->id : $item->service}}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->category }}</td>
<td>{{ isset($item->rate) ? $item->rate : $item->price_per_k }} $</td>
<td>{{ $item->min }}</td>
<td>{{ $item->max }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
And a part its controller:
public function apiService(){
$api = ApiSetting::first();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"api_tokens=".$api->key."&action=services");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$items = json_decode($server_output);
return view('admin.apiServices', compact('items'));
}
There is not enough information in your question.
It looks to me like you are attempting to access a relational object without setting up the relationship in your model.
I don't know how you want to access services from an item but you may want something like this in your Item model
public function Service()
{
return $this->hasmany('App\Service');
}
Please reference: https://laravel.com/docs/5.7/eloquent-relationships