2017-03-07 1 views
0

나는 검색 결과와 동일한 레코드를 반환하는 검색 필드를 만들려고합니다. 나는 2 개의 모델, 상표와 차를 가지고있다. 내 관계는 브랜드 (Opel, Suzuki 등)는 많은 자동차를 보유하고 있으며 자동차는 브랜드에 속한다는 것입니다.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' 
    ]; 
} 

나는 몇 가지 정보와 모든 자동차를 표시하는 페이지가 있습니다.

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

마지막으로 내 컨트롤러 파일
경로 : CarController.php

<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> 

이 내 경로 파일입니다 Find Cars form

이 페이지의 드 코드는

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

else if 문이 완벽하게 작동합니다. 정수 (연도)를 채우면 연도 검색을 사용하고 문자열이 채워지면 브랜드 검색을 사용합니다.

그러나 브랜드 검색은 완벽하게 작동합니다. 'Opel'을 검색하면 Opel이라는 브랜드의 정보가 담긴 모든 차량을 갖게됩니다. 그러나 1990 년을 검색 할 때, 나는 1990 년 자동차를 가진 브랜드를 얻었고 그 다음 다른 브랜드의 자동차도 얻습니다.

예 :

I 1990를 검색 할 경우, 나는 3 차 얻을 : 오펠 (1990), 오펠 (2000)와 오펠 (1882).

다른 예 : 나는 2000 년을 검색 할 때 Opel (2000), Opel (1990), Opel (1882), BMW (2000), BMW (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

감사합니다! 그것은 효과가 있었다. –

관련 문제