使用 Laravel APi 时搜索 Returns 数据库中的所有数据
Search Returns All Data in Database while using Laravel APi
我正在使用 Laravel 处理 API,我希望能够使用 API 进行搜索,但我无法 这是我的方法
public function searchapi()
{
$search = request()->get('search');
/* $books = Book::when($search, function ($query, $search) {
return $query->where('name', 'LIKE', "%{$search}%");
})
->orderBy('created_at', 'desc')
->simplepaginate(12); */
$books = Book::where('name', 'LIKE', "%{$search}%")->Paginate(16);
$author = Author::where('name', 'LIKE', "%{$search}%")->Paginate(16);
$genre = Category::where('title', 'LIKE', "%{$search}%")->Paginate(16);
return Response::json(array(
'books' => $books,
'author'=>$author,
'genre'=>$genre,
));
}
我不知道我做错了什么,因为 returns 使用 postman 搜索时数据库中的所有数据。
我的路线在api.php
Route::get('/search', 'BooksController@searchapi');
更新
我正在使用 post man 进行测试。我通过了Key=search
和 value=searchterm
方法:
public function searchapi(Request $request)
{
$search = request()->get('search');
/* $books = Book::when($search, function ($query, $search) {
return $query->where('name', 'LIKE', "%{$search}%");
})
->orderBy('created_at', 'desc')
->simplepaginate(12); */
$books = Book::where('name', 'LIKE', "%{$search}%")->Paginate(16);
$author = Author::where('name', 'LIKE', "%{$search}%")->Paginate(16);
$genre = Category::where('title', 'LIKE', "%{$search}%")->Paginate(16);
return Response::json(array(
'books' => $books,
'author'=>$author,
'genre'=>$genre,
));
}
路线:
Route::post('/search', 'BooksController@searchapi');