2017-04-13 125 views
-2

我是laravel的新手,嘗試做一個搜索功能,然後我遇到了代碼返回空白頁的問題&它假設返回一個帶有結果的表laravel 5.4函數返回空白頁

這是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','[email protected]'); 

Route::get('searchbook', function() { 
    return view('book-search'); 
}); 
+1

活計,你需要學習laravel第一 這不是我們這裏是用laravel方式一個美妙的免費教程 https://laracasts.com/series/laravel-from-scratch-2017/ – Cybersupernova

回答

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')); 
}