2017-03-07 39 views
0

即時嘗試使搜索字段返回等於搜索結果的記錄。我有2款車型,品牌和汽車。我的關係是一個品牌(歐寶,鈴木等)有很多車,一輛車屬於一個品牌。Laravel 5在關係中的雄辯搜索不起作用

品牌型號

class Brand extends Model 
{ 
    protected $fillable = [ 
    'brandname' 
    ]; 
    public function cars(){ 
     return $this->hasMany(Car::class); 
    } 

} 

汽車型號

class Car extends Model 
{ 
    public function brand(){ 
     return $this->belongsTo(Brand::class); 
    } 
    protected $fillable = [ 
     'user_id', 'brand_id', 'year', 'licensplate' 
    ]; 
} 

我有一個頁面,顯示所有的一些信息汽車。 Find Cars form

這是網頁的代碼德:

<div class="col-sm-8 blog-main"> 
    <h1>Alle auto's</h1><hr> 
    <form method="POST" action="/allcars"> 
     {{csrf_field()}} 
     <div class="form-group"> 
      <input type="text" name="searchfield" placeholder="Merk"> 
      <button type="submit">Zoek</button> 
     </div> 
    </form> 
    @foreach($brands as $brand) 
      @foreach($brand->cars as $car) 
       <div class="form-group"> 
        <h5> 
         {{$brand->brandname}} 
        </h5> 
        Kenteken: {{$car->licensplate}}<br> 
        Bouwjaar: {{$car->year}}<br> 
       </div> 
      @endforeach 
    @endforeach 
</div> 

這是我的路線文件:
路線

Route::post('/allcars', '[email protected]'); 

最後我控制文件: CarController.php

public function show(){ 
    $input = request('searchfield'); 
    if(empty($input)) 
    { 
     return back(); 
    } 

    else if(is_numeric($input)) 
    { 
     // Search on year 
     $brands = Brand::with('cars')->whereHas('cars', function($q) 
     { 
      $input = request('searchfield'); 
      $q->where('year', $input); 
     })->get(); 
     return view('content.cars.index', compact('brands')); 
    } 
    else 
    { 
     // Search on brandname 
     $brands = Brand::with('cars')->get()->where('brandname', $input); 
     return view('content.cars.index', compact('brands')); 
    } 
} 

其他if語句完美工作。當一個整數(年)被填入時,它使用年份搜索,並填充字符串時使用品牌搜索。

但是,品牌搜索工作正常。如果我搜索'歐寶',我會從歐寶品牌獲得所有的汽車。但是當我在1990年進行搜索時,我得到了一輛擁有1990年車型的品牌,然後我也從該品牌購買了其他車型。

例子:

如果我要尋找的1990年,我得到3輛汽車:歐寶(1990年),歐寶(2000年)和歐寶(1882年)。 (2000),歐寶(1990),歐寶(1882),寶馬(2000),寶馬(1721)等5款車型。

我想我在代碼中做錯了什麼,請糾正我。 這是我的移民BTW: 汽車遷移

Schema::create('cars', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->unsignedInteger('user_id'); 
     $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
     $table->unsignedInteger('brand_id'); 
     $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade'); 
     $table->string('year'); 
     $table->string('licensplate'); 
     $table->timestamps(); 
    }); 

品牌融合

Schema::create('brands', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('brandname'); 
     $table->timestamps(); 
    }); 

回答

0

嘗試改變否則,如果部分是:

else if(is_numeric($input)) 
    { 
     // Search on year 
     $brands = Brand::with(['cars' => function ($q) use ($input) { 
      $q->where('year',$input); 
     }])->get(); 

     return view('content.cars.index', compact('brands')); 
    } 
+0

謝謝!有效。 –