找到你要的答案

Q:Laravel 5 pagination with orderBy

Q:5分页与OrderBy laravel

I have been trying to paginate Eloquent result in laravel 5. But I'm facing a problem.

Following code works

$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);

But when I try to order the result, it doesnt work

    $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);

It throws me Method Illuminate\View\View::__toString() must not throw an exception. How to fix this?

Note : $this->brand holds the instance of Brand Model

我一直试图将在laravel 5雄辩的结果。但我面临着一个问题。

以下的代码

$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);

它把我的方法阐明\观观::__tostring()不抛出异常。如何解决这个问题?

注:美元- >;品牌持有品牌模型实例

answer1: 回答1:

Can you try something like...

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

你能试试类似的东西吗…

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

This Error can occur in Following situations:

Situation 1: Trying to print out a value in an array.

Answer 1: Try printing out the array. Are you sure it's an array? I've gotten this error when it was an object instead of an array. Try doing a print_r and seeing what you get.

Situation 2: You have this associated array like this:

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
                    )

            )

    )

When you try to access post_images array directly within a View, it throws an error. No. Matter. What. You. Do.

Answer 2: Check in all the places where you are calling the View. What happened here is that I was trying to access the same view somewhere else in an area where I wasn't giving the post_images array. Took FOREVER to figure out.

这种错误可能发生在下列情况:

情景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阵列的其他地方相同的观点。花了永远的揣摩。

php  laravel  pagination  eloquent  laravel-5