でlaravel検索を取得する私は現在、私は私のアプリでtitle,category & location
フィールドを持っていると私は、ユーザーが選択することで、結果を取得できるようにしたい、マルチオプションで私のウェブサイトの検索結果を取得したい、マルチオプション
をlaravel 5.4を使用しています例えば、あるユーザがロケーションベース検索を選択しただけで、他のオプションを満たしていないというエラーを出さずに結果を得ることができる場合など、これらのフィールドのいずれか。
問題。 私がタイトルフィールドだけで作業する前に、うまくいきましたが、他の2つのオプションを定義すると、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', '[email protected]');
私の見解:
@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>
あなたのコントローラを見ると、あなたは関係を利用していません。モデル関係と検索フォームを共有します。 – Sandeesh
テーブル内に外部キーの宣言が見つかりません。広告モデルでの場所とカテゴリの外部キーとは何ですか? – Muthu17
これらはIDのDBタブです。 – djhru