面对问题:方法Illuminate \ Database \ Query \ Builder :: links不存在

时间:2018-08-15 10:43:57

标签: php laravel

嘿,每个人我都在创建一个laravel项目,我想添加分页,但是我遇到了这个错误:

  

方法Illuminate \ Database \ Query \ Builder :: links不存在。 (视图:   C:\ xampp \ htdocs \ TestingTask \ resources \ views \ income \ index.blade.php)

这是控制器代码:

public function index()
    {

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

    }

查看代码:

@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 个答案:

答案 0 :(得分:0)

您的代码中有错字。将$income更改为$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

答案 1 :(得分:0)

这是因为您的foreach循环变量的更改如下

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