【问题标题】:Laravel 5 pagination with orderByLaravel 5 使用 orderBy 分页
【发布时间】:2015-06-23 10:11:26
【问题描述】:

我一直在尝试在 laravel 5 中对 Eloquent 结果进行分页。但我遇到了一个问题。

以下代码有效

$brand = $this->brand->select('id', 'label', 'slug', 'logo', 'banner_image', \DB::raw('CASE WHEN status = 0 THEN "Disabled" else "Enabled" END as status'));

    if($filter) {
        $brand = $brand->where('label', '=', $filter);
    }

    return  $brand->paginate($show);

但是当我尝试订购结果时,它不起作用

    $brand = $this->brand->select('id', 'label', 'slug', 'logo', 'banner_image', \DB::raw('CASE WHEN status = 0 THEN "Disabled" else "Enabled" END as status'));

    if($filter) {
        $brand = $brand->where('label', '=', $filter);
    }

    return  $brand->orderBy('id', 'DESC')->paginate($show);

它抛出了我方法 Illuminate\View\View::__toString() 不能抛出异常。如何解决这个问题?

注意:$this->brand 包含 Brand Model 的实例

【问题讨论】:

  • 您正在尝试将 View 对象转换为字符串,这就是您收到此错误的原因。请在返回值上执行dd() 并发布您的查看代码。
  • 谢谢@akad0。是的,似乎我在处理从模型收到的详细信息时犯了一个错误。万分感谢。 :)
  • 没问题,很高兴我能帮上忙

标签: php laravel pagination eloquent laravel-5


【解决方案1】:

你可以试试...

$data = brand->orderBy('id', 'DESC')->paginate($show);
return View::make('your_view')->with($data);

【讨论】:

    【解决方案2】:

    此错误可能发生在以下情况:

    情况 1:试图打印出数组中的值。

    答案 1:尝试打印出数组。你确定它是一个数组?当它是一个对象而不是数组时,我得到了这个错误。试着做一个 print_r 看看你得到了什么。

    情况 2:您有这样的关联数组:

    Array
        (
            [post_id] => 65
            [post_text] => Multiple Images!
            [created_at] => 2014-10-23 09:16:46
            [updated_on] => 
            [post_category] => stdClass Object
                (
                    [category_label] => Help Wanted
                    [category_code] => help_wanted
                )
    
            [employee_full_name] => Sam Jones
            [employee_pic] => /images/employee-image-placeholder.png
            [employee_email] => jon@gmail.com
            [post_images] => Array
                (
                    [0] => stdClass Object
                        (
                            [image_path] => 9452photo_2.JPG
                        )
    
                    [1] => stdClass Object
                        (
                            [image_path] => 8031photo_3.JPG
                        )
    
                )
    
        )
    

    当您尝试直接在视图中访问 post_images 数组时,会引发错误。不管。什么。你。做。

    答案 2:检查您调用视图的所有位置。这里发生的事情是我试图在我没有提供 post_images 数组的区域的其他地方访问相同的视图。花了 FOREVER 才弄明白。

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2018-09-16
      • 2017-03-25
      • 2021-10-13
      • 2016-01-22
      • 2015-05-07
      • 2015-08-06
      • 2022-01-16
      • 2017-01-03
      相关资源
      最近更新 更多