Anirudh Lou
Anirudh Lou

Reputation: 831

How to resolve Undefined method links() in Laravel?

I am using Laravel 6.0 and on my index.blade.php, I am calling the links via {{ $countries->links() }} but I get this exception message:

Call to undefined method App\Country::links() (View: C:\wamp\www\projectnoah\resources\views\country\index.blade.php)

In my CountryController I have defined pagination which is something like this:

public function index()
{
    $countries = Country::orderBy('country_name', 'asc')->paginate(100);
    return view('country.index', compact('countries'));
}

EDIT

This is my blade:

@extends('layouts.noah')

@section('title', 'NOAH | Countries List')

@section('content')

<div class="lt-menu" id="lt-menu">
    @include('layouts.sidebar.settings')
</div>

<div class="lt-content" id="lt-content">
    <input type="hidden" name="count" value="{{ count($countries) }}" id="countries-count">
    <div class="row z-depth-2">
        <div class="row">
            <div class="col s6 m6 l6">
                <div class="btn-group" role="group" aria-label="Basic example">
                    <a href="countries/create" class="btn btn-small" style="border-radius: 0;"><i class="fas fa-plus"></i><span class="button-add"></span> Create</a>
                </div>
            </div>
            <div class="col s6 m6 l6 right-align">
                <div class="btn-group" role="group" aria-label="Basic example">
                    <a href="/countries" class="btn cyan btn-small" style="border-radius: 0;"><i class="fas fa-list"></i></a>
                </div>
            </div>
        </div>
        <div class="row">
            <nav>
                <div class="nav-wrapper" style="color: blue;">
                    <div class="col s12">
                        <a href="#!" style="color: green;">Countries</a>
                    </div>
                </div>
            </nav>
        </div>
    </div>
    <div class="container z-depth-1">
        <div class="lt-list-contents z-depth-1">
            @include('./inc/messages')
            <div class="col s12 m12 l12">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th width="70px" class="center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @if (count($countries) > 0)
                        @foreach($countries as $key => $countries)
                        <tr>
                            <td>{{ $countries->country_name }}</td>
                            <td>
                                <a href="{{ URL::to('countries/' . $countries->id) }}"><i class="fas fa-eye cyan-text"></i></a>

                                <a href="{{ URL::to('countries/' . $countries->id . '/edit') }}"><i class="fas fa-pencil-alt blue-grey-text"></i></a>

                                <a href="{{ URL::to('/countries/'. $countries->id) }}"><i class="fas fa-trash red-text"></i></a>  
                            </td>
                        </tr>
                        @endforeach
                        @else
                        <tr>
                            <td colspan="4" style="letter-spacing: 5px;" class="center">No countriess Found!!!</td>
                        </tr>
                        @endif

                    </tbody>
                </table>
            </div>
        </div>
        <div class="col s12 m12 l12 body-reset">
            {{ $countries->links() }}
        </div>  
    </div>

</div>

@endsection

Any idea on how to fix this? This works fine in my other model.

Upvotes: 0

Views: 790

Answers (1)

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

Issue is in @foreach loop your alias name and array is the same so change it.

 @foreach($countries as $key => $country) <!-- Here change it as $country --->

 @endforeach

 {{ $countries->links() }}

Upvotes: 2

Related Questions