Route::controller('product', 'ProductController');
这是我在ProductController中的代码。
public function getData($search=null)
{
if($search == null){
$products = Prod::paginate($limit = 10)->toJson();
return $products;
}else{
$products = Prod::where('name', 'LIKE', '%'.$search.'%')
->paginate(10)
->toJson();
return $products;
}
}
然后我尝试预览结果,但这就是我得到的结果。
当我尝试/ project / public / product / data?search = 12
时{"total":72,"per_page":10,"current_page":1,"last_page":8,"from":1,"to":10,"data":[
{"id":148,"name":"tester28","price":1250,"brand_id":5,"category_id":5,"availability":1,"product_pic_id":5,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":149,"name":"tester29","price":1371,"brand_id":1,"category_id":6,"availability":1,"product_pic_id":3,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":150,"name":"tester30","price":1351,"brand_id":4,"category_id":5,"availability":1,"product_pic_id":1,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":151,"name":"tester31","price":374,"brand_id":7,"category_id":8,"availability":1,"product_pic_id":2,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 08:54:53"},{"id":152,"name":"tester32","price":1974,"brand_id":9,"category_id":4,"availability":1,"product_pic_id":2,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":153,"name":"tester33","price":2435,"brand_id":3,"category_id":8,"availability":1,"product_pic_id":1,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":154,"name":"tester34","price":2392,"brand_id":8,"category_id":9,"availability":1,"product_pic_id":6,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":155,"name":"tester35","price":1029,"brand_id":6,"category_id":3,"availability":1,"product_pic_id":7,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":156,"name":"tester36","price":1882,"brand_id":7,"category_id":8,"availability":1,"product_pic_id":10,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"},
{"id":157,"name":"tester37","price":1233,"brand_id":4,"category_id":10,"availability":1,"product_pic_id":6,"created_at":"2014-03-09 05:31:14","updated_at":"2014-03-09 05:31:14"}]}
我不知道他们为什么不被过滤。谢谢你的帮助:D
答案 0 :(得分:0)
问题主要在于$ search不包含您的想法。只有当您有一条路线告诉它使用部分URL作为参数时,控制器上的参数才会被填充,例如:
Route::get('users/view/{id}', ['uses' => 'MyController@MyAction']);
因此,在您的控制器中,您应该只是查找输入参数。
public function getData()
{
$search = Input::get('search');
if(!$search)
{
$products = Prod::paginate($limit = 10)->toJson();
return $products;
}
else
{
$products = Prod::where('name', 'LIKE', '%'.$search.'%')
->paginate(10)
->toJson();
return $products;
}
}