【问题标题】:Method Illuminate\Database\Eloquent\Collection::currentPage does not exist [closed]方法 Illuminate\Database\Eloquent\Collection::currentPage 不存在 [关闭]
【发布时间】:2020-01-25 23:54:20
【问题描述】:

orderproduct 等其他资源路由控制器索引功能正在工作并显示索引页面 但是当我请求类别索引页面时,laravel 5.8 说

方法 Illuminate\Database\Eloquent\Collection::currentPage 不存在 view categories/index.php

这是所有其他路由都在使用的路由组,除了用于从索引功能显示索引页面的类别控制器和用于索引功能的仪表板

Route::group(['prefix' => 'dashboard', 'namespace' => 'Dashboard', 'middlware' => ['auth:admin']], function () {
    Route::name('dashboard.')->group(function () {
        Route::resource('/', 'DashboardController');
        Route::resource('/products', 'ProductController'); // ->except(['create', 'index']);
        Route::resource('/categories', 'categoryController'); // ->except(['create', 'index']);
        Route::resource('/orders', 'orderController'); // ->except(['create', 'index']);
    });
});

它们运行良好,但是当我配置多重身份验证并进行管理员身份验证时,它开始显示这些错误 如果有人在 laravel5.8 工作,请提供帮助

【问题讨论】:

  • 问题不在于路由,而在于方法 currentPage 不存在。另外,你有错字。 web.php中的categoryController但文件实名是categorycontroller
  • 您收到 BadMethodCallException。可能是你的表单动作路由不正确或者web.php中的路由不正确。
  • Rashed Hasan 先生,我已经通过 php artisan route:list 和搜索文件检查了我的路线,它们都是正确的
  • #Arturs Jerjomins 先生,我已经检查了这个拼写错误,但仍然显示问题。我认为这不会造成问题,因为在这个控制器中有一个添加类别方法,它显示页面以添加新类别,这种方法运行良好,只有索引方法没有捕获
  • 刀片文件有问题。分享你的控制器索引方法和索引刀片文件。

标签: php laravel authentication laravel-5.8 laravel-6


【解决方案1】:

您从collection 实例而不是LengthAwarePaginator 实例上的方法静态调用currentPage 属性

您的控制器必须返回一个分页查询构建器实例 Illuminate\Pagination\LengthAwarePaginator 类似

class categoryController extends Controller
{
   public function index()
   {
      $categories = \DB::table('categories')->orderByDesc('id')->paginate(4);
      return view('dashboard.categories.index', compact('categories'));
   }
}

然后在您的 dashboard/categories/index 视图中,使用 currentPage() 作为函数

{{ $categories->currentPage() }}

来自docs

PS:不要只是复制粘贴这段代码,它可能会导致其他错误,因为我只是对你的控制器的外观做出了最好的猜测

【讨论】:

  • Caddy DZ 谢谢,请看下面我的回答
【解决方案2】:

zahid hasan emon bro 这是我的控制器

class categorycontroller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = [];
//        $data['categories'] = \DB::table('categories')->get();
        $data['categories'] = \DB::table('categories')->orderByDesc('id')->paginate(4);
        return view('dashboard.categories.index',$data);
    }

这是我的分类页面

    @section('page-subtitle','List of all categories')

@section('content')
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title"></h3>
            <a href="{{route('dashboard.categories.create')}}" class="btn btn-primary">New Category</a>
            <div class="box-tools pull-right">
                {{(($categories->currentPage() * $categories->perPage())- $categories->perPage() )+1 }}
                of {{$categories->total()}}
            </div>
        </div>
        <div class="box-body">
            <table class="table table-condensed table-striped table-responsive">
                <tr>
                    <td>ID</td>
                    <td>Name</td>
                    <td>Action</td>
                </tr>
                @forelse($categories as $category)
                    <tr>
                        <td>{{$category->id}}</td>
                        <td>{{$category->name}}</td>
                        <td>
                            <div class="action-box">
                                <a href="{{route('dashboard.categories.edit',$category->id)}}"><i class="fa fa-edit"></i></a>
                                <a href=""><i class="fa fa-remove"></i></a>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr class="text-center warning">
                        <td colspan="3">No Record Found</td>
                    </tr>
                @endforelse
            </table>
        </div>

        <!-- /.box-body -->
        <div class="box-footer">
            @if(count($categories))
                <div class="pull-left">
                    {{$categories->links()}}
                </div>
            @endif

        </div>
        <!-- /.box-footer-->
    </div>
    <!-- /.box -->
@endsection

我认为我用于显示页数的 currentpage() 方法存在问题,但在进行多重身份验证之前效果很好

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2020-11-21
    • 2021-09-08
    • 2020-11-13
    • 2019-12-11
    • 2020-02-04
    • 2019-11-12
    • 2019-05-24
    相关资源
    最近更新 更多