我一直试图在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);
它抛出了我方法照明\ View \ View :: __ toString()不能抛出异常。如何解决这个问题?
注意:$ this->品牌持有品牌模型的实例
答案 0 :(得分:0)
你可以试试像......
$data = brand->orderBy('id', 'DESC')->paginate($show);
return View::make('your_view')->with($data);
答案 1 :(得分:0)
以下情况可能会发生此错误:
情况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
)
)
)
当您尝试直接在View中访问post_images数组时,会引发错误。不管。什么。您。做。
答案2:检查您呼叫视图的所有地方。这里发生的事情是我试图在我没有给出post_images数组的区域中的其他地方访问相同的视图。永远想知道。