我是laravel的新手,并尝试制作搜索功能,然后我遇到了代码返回空白页面的问题。它假设返回一个包含结果的表
这是BookSearchController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Book_Search;
class BookSearchController extends Controller
{
public function searchForm(){
return view('book-search');
}
public function searchbook(Request $request){
$reader = new Book_Search;
$reader->searchbooks($request);
}
}
这是Book_Search模型
<?php
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests;
use DB;
class Book_Search extends Model
{
public function searchbooks(Request $request){
//$query = $request->all();
$query = $request->get('query');
$Books = DB::table('bookuser')->Join('users', 'bookuser.userID', '=','users.id')->Join('book', 'bookuser.bookID','=','book.ID')->select('Firstname','picture','price','availabilty','name')->where('name','like','%'. $query. '%')->get();
return view('book_search', compact('Books', 'query'));
}
}
接受查询的表单
<html>
<head>
<title>Book | Search</title>
</head>
<body>
<form action = "/searchingg" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<table>
<tr>
<td>Name</td>
<td><input type='text' name='query' /></td>
</tr>
<tr>
<td colspan = '2'>
<input type = 'submit' value = "search"/>
</td>
</tr>
</table>
</form>
</body>
</html>
假设查看结果的表单
<html>
<head>
<title>Search Student Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>User Name</td>
<td>Picture</td>
<td>Price</td>
<td>Availability</td>
<td>Book Name</td>
</tr>
<?php
foreach ($Books as $Book){
?>
<tr>
<td><?php echo $Book->Firstname?></td>
<td><?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $Book->picture ).'"/>';?></td>
<td><?php echo $Book->price?></td>
<td><?php echo $Book->availabilty?></td>
<td><?php echo $Book->name?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
路线
Route::post('searchingg','BookSearchController@searchbook');
Route::get('searchbook', function () {
return view('book-search');
});
答案 0 :(得分:0)
您的控制器应负责将数据返回给客户端。
更改模型以从数据库返回结果集:
public function searchbooks(Request $request){
$query = $request->get('query');
$Books = DB::table('bookuser')->Join('users', 'bookuser.userID', '=','users.id')->Join('book', 'bookuser.bookID','=','book.ID')->select('Firstname','picture','price','availabilty','name')->where('name','like','%'. $query. '%')->get();
return $Books;
}
和您的控制人员:
public function searchbook(Request $request){
$reader = new Book_Search;
$Books = $reader->searchbooks($request);
$query = $request->query;
return view('book_search', compact('Books', 'query'));
}