Im writing a small program where i want to show all location depending on the current user customer_id. In this logic customers are company's buying from us so a customer isnt the same as a user. THe customer can have multiple locations and i want to show the locations depending on the current user his customer_id.
To accomplish this i wrote this code: This is the code were the error is thrown.
if (isset(auth()->user()->customer_id) && !auth()->user()->hasRole('admin')) {
$customer = Customer::find(auth()->user()->customer_id);
$locations = $customer->locations()->get();
} else {
$locations = Location::all();
}
return view('locations.index', ['locations' => $locations]);
I check if the user has a customer_id and isnt a admin. If both are true i get the customer connected to the customer_id. Then i retrieve all locations related to that customer. This all works and when i check it with dd($var) it always returns the expected values.
index.blade.php
<table id="locations" class="table align-items-center table-flush" style="100%">
<thead class="thead-light">
<tr>
<th scope="col">{{ __('Customer') }}</th>
<th scope="col">{{ __('Location Name') }}</th>
<th scope="col">{{ __('Address') }}</th>
<th scope="col">{{ __('Postal Code') }}</th>
<th scope="col">{{ __('City') }}</th>
<th scope="col">{{ __('Country') }}</th>
</tr>
</thead>
<tbody>
@foreach ($locations as $location)
<tr>
<td>{{ $location->customer->name}}</td>
<td>{{ $location->name}}</td>
<td>{{ $location->address}}</td>
<td>{{ $location->postalcode}}</td>
<td>{{ $location->city}}</td>
<td>{{ $location->country}}</td>
</tr>
@endforeach
</tbody>
</table>
Now when i return the view i get the following error:
Method Illuminate\Database\Eloquent\Collection::links does not exist
The strange thing here is that i already return data with other entities in the same way and that works fine. This code does the same thing and actually works.
if (isset(auth()->user()->distributor_id) && !auth()->user()->hasRole('admin')) {
$distributor = Distributor::find(auth()->user()->distributor_id);
$customer = $distributor->customers()->get();
} else {
$customer = Customer::all();
}
return view('customers.index', ['customers' => $customer]);
After looking around on the internet and SO i found this question:
Laravel 4 Call to undefined method Illuminate\Database\Eloquent\Collection::links(). The question looks a lot like mine but the awnser doesnt solve my problem. I have tried to implement their awnser but i still get the same exception.
Can anyone explain why i get this error?
links
there and that is causing the problem – zahid hasan emon$locations
array anylinks
element available? – Amit Senjaliya