我正在使用laravel 5.4,
我想通过多个选项获取我的网站搜索结果,目前我的应用中有title,category & location
个字段,我想让用户通过选择任何这些字段来获得结果,例如,如果一个用户只选择了位置库搜索能够获得结果而不会出现不填写其他选项的错误等。
问题;
在我只处理标题字段之前,一切正常,但是当我定义其他两个选项categories & locations
时,结果将不再返回,我的错误在哪里?
这是我的控制者:
public function search() {
$q = Input::get('q');
$ads = null;
$categories = null;
$locations = null;
if($q) {
$categories = Category::where('name','LIKE','%'.$q.'%')
->paginate(2)
->appends('q', $q);
}
if($q) {
$locations = Location::where('name','LIKE','%'.$q.'%')
->paginate(2)
->appends('q', $q);
}
if($q) {
$ads = Ad::where('title','LIKE','%'.$q.'%')
->orWhere('description','LIKE','%'.$q.'%')
->paginate(2)
->appends('q', $q);
}
return view('search-result', compact('ads', 'categories', 'locations', 'q'));
}
我的路线:
//search routes
Route::any('/search', 'HomeController@search');
我的观点:
@extends('layouts.view')
@section('content')
@if(isset($ads))
<h2>Search results</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($ads as $ad)
<tr>
<td>{{$ad->title}}</td>
<td>{{$ad->description}}</td>
</tr>
@endforeach
</tbody>
</table>
@else
no results!
@endif
@endsection
更新
我的控制器关系:
use Illuminate\Http\Request;
use App\Events\StudentAdded;
use App\Ad;
use App\Company;
use App\Location;
use App\Category;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;
型号: 地点模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
protected $fillable = [
'name',
];
public function ads(){
return $this->hasMany(Ad::class);
}
}
类别模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function ads(){
return $this->hasMany(Ad::class);
}
}
广告模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ad extends Model
{
protected $fillable = [
'company_id', 'title', 'slug', 'image', 'description', 'address', 'job_title', 'salary',
];
public function company(){
return $this->belongsTo(Company::class);
}
public function category(){
return $this->belongsTo(Category::class);
}
public function location(){
return $this->belongsTo(Location::class);
}
public function employment(){
return $this->belongsTo(Employment::class);
}
}
更新2:
搜索表单:
<form class="form-inline myform" action="/search" method="POST" role="search">
{{ csrf_field() }}
<div class="input-group" data-toggle="tooltip" data-placement="top" title="Title">
<span class="input-group-addon" id="basic-addon1"><i class="fa fa-pencil"></i></span>
<input name="q" type="text" class="form-control" placeholder="Title" aria-describedby="basic-addon1">
</div>
<label data-toggle="tooltip" data-placement="top" title="select category" class="sr-only mr-sm-2" for="inlineFormCustomSelect">Category</label>
<select name="q" data-toggle="tooltip" data-placement="top" title="select category" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
<label data-toggle="tooltip" data-placement="top" title="select City" class="sr-only mr-sm-2" for="inlineFormCustomSelect">City</label>
<select name="q" data-toggle="tooltip" data-placement="top" title="select City" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
@foreach ($locations as $location)
<option value="{{ $location->id }}">{{ $location->name }}</option>
@endforeach
</select>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
答案 0 :(得分:1)
由于您要将搜索查询附加到分页链接,因此您应该在表单中使用GET
。其次,您尝试传递所有具有相同名称的搜索输入。我已经更改了输入名称以使搜索更容易。我在下面显示的代码使用3个输入来制作查询以获取适当的广告。当提供3个输入中的任何一个时,它用于从数据库中获取广告。当提供的输入超过1时,我们将应用所有条件。
查看
<form class="form-inline myform" action="/search" method="GET" role="search">
{{ csrf_field() }}
<div class="input-group" data-toggle="tooltip" data-placement="top" title="Title">
<span class="input-group-addon" id="basic-addon1"><i class="fa fa-pencil"></i></span>
<input name="title" type="text" class="form-control" placeholder="Title" aria-describedby="basic-addon1">
</div>
<label data-toggle="tooltip" data-placement="top" title="select category" class="sr-only mr-sm-2"
for="inlineFormCustomSelect">Category</label>
<select name="category" data-toggle="tooltip" data-placement="top" title="select category"
class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
<option value="">Select category</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
<label data-toggle="tooltip" data-placement="top" title="select City" class="sr-only mr-sm-2"
for="inlineFormCustomSelect">City</label>
<select name="location" data-toggle="tooltip" data-placement="top" title="select City"
class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
<option value="">Select Location</option>
@foreach ($locations as $location)
<option value="{{ $location->id }}">{{ $location->name }}</option>
@endforeach
</select>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
控制器
public function search()
{
$searchTitle = request('title');
$searchCategory = request('category');
$searchLocation = request('location');
$ads = null;
if($searchTitle || $searchCategory || $searchLocation) {
$ads = Ad::when($searchTitle, function ($query) use ($searchTitle) {
return $query->where('title', 'like', "%{$searchTitle}%")
->orWhere('description', 'like', "%{$searchTitle}%");
})
->when($searchCategory, function ($query) use ($searchCategory) {
return $query->whereHas('category', function ($query) use ($searchCategory) {
$query->where('id', $searchCategory);
});
})
->when($searchLocation, function ($query) use ($searchLocation) {
return $query->whereHas('location', function ($query) use ($searchLocation) {
$query->where('id', $searchLocation);
});
})
->paginate(2)
->appends(request()->query());
}
return view('search-result', compact('ads'));
}
我不确定您是否需要搜索结果页面中的类别和位置。但是,如果您需要它们,则可以使用广告关系获取或根据输入值获取所有位置。这很简单,我认为你可以做到这一点。