2
votes

Hey everyone I was creating a laravel project and I wanted to add the pagination but I faced this error :

Method Illuminate\Database\Query\Builder::links does not exist. (View: C:\xampp\htdocs\TestingTask\resources\views\income\index.blade.php)

Here is the controller code:

public function index()
    {

        $income =Income::orderBy('created_at','desc')->paginate(1);
        return view('income.index', compact('income'));

    }

The view code:

@extends('layouts.app')
@section('content')
<h3>Profit</h3>
<br>
@if(count($income)>0)
@foreach($income as $income)
    <div class="card bg-muted">
    <h3><a href="/income/{{$income->id}}">{{$income->title}}</a></h3>
    </div>
@endforeach
    {{$income->links()}}
@else
<p>No result</p>

@endif
@stop
2
Can you change your foreach variable to foreach($income as $single_income)Ayaz Ali Shah
@AyazShah, yes Ayaz you were right I changed and all worked perfectly:)Mirasan
Sure I have also added an answer kindly check it. It may useful for youAyaz Ali Shah

2 Answers

0
votes

You have a typo in your code. change $income to $inc.

@extends('layouts.app')
@section('content')
<h3>Profit</h3>
<br>
@if(count($income)>0)
@foreach($income as $inc)
    <div class="card bg-muted">
    <h3><a href="/income/{{$inc->id}}">{{$inc->title}}</a></h3>
    </div>
@endforeach
    {{$income->links()}}
@else
<p>No result</p>

@endif
@stop
0
votes

This is because of your foreach loop variable kindly change as following

@if(count($income)>0)
@foreach($income as $income_single)
...
@endforeach
    {{$income->links()}}
@else