我想爲我的網站創建高級搜索。高級搜索laravel中的多對多關係
我有有許多與書一對多的關係(我的主要模式)
類我怎麼能在形式上通過分類搜索與選擇
我的代碼在控制器
public function advancedSearch(Request $request){
$book = Book::query();
if($request->get('name')){
$name = $request->get('name');
$book->where('name', 'like', '%'.$name.'%');
}
if($request->get('author')){
$author = $request->get('author');
$book->where('author', 'like', '%'.$author.'%');
}
if($request->get('category')){
// MY QUESTION
}
$books = $book->get();
$category = Category::lists('name','id');
return view('search')->with('book',$books)->with('title','Search')->with('category',$category);
}
和我的表單是
{!! Form::open(['method' => 'get' , 'url' => '/search/advanced']) !!}
<div class="form-group col-md-3">
{!! Form::text('name',Input::old('name'),['class' => 'form-control input-sm col-md-3','placeholder'=>'Name']) !!}
</div>
<div class="form-group col-md-3">
{!! Form::text('author',Input::old('author'),['class' => 'form-control input-sm col-md-3','placeholder'=>'Author']) !!}
</div>
<div class="form-group col-md-3">
{!! Form::select('category[]',$category,'Select Category',['class' => 'form-control selectpicker input-sm col-md-3','placeholder'=>'Author','multiple']) !!}
</div>
<input class="btn btn-default" type="submit" name="btn" value="search">
{!! Form::close() !!}
什麼是書籍和類別 – chanafdo
@chanafdo它的多對多 –