0
votes

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?

3
are you use this variable for pagination?sandip bharadva
add your blade file too. you are using links there and that is causing the problemzahid hasan emon
No, im just returning the variable to the view and looping through to show the data. Like i said, in the second block of code it works fine.Collin
@Collin In your $locations array any links element available?Amit Senjaliya
@AmitSenjaliya No, there aren't any links elements availableCollin

3 Answers

0
votes

Maybe this work

$locations = $customer->locations->get();

remove parentheses after locations and you need to define function locations in your Customer.php

public function locations()
{
    return $this->hasMany('App\Locations', 'user_id', 'id');
}
0
votes

It seems that in your template file, you call links method for locations. This method is used for pagination implementation. So, you should add paginate method to access links method.

try this one:

$customer = Customer::where('id', auth()->user()->customer_id)->paginate(10);
0
votes

you should use paginate($numerOfLocationPerPage) instead of get()

in $locations = $customer->locations()->get();