2017-10-06 1 views
-2

내 검색 기능에서 조인 방법을 사용하고 있기 때문에 내 앱에서 게시물 제목과 게시물 태그를 동시에 검색하려고합니다. 여기 가입 방법으로 Laravel 검색

내 형태 :

<div class="search"> 
     <form class="form-inline" action="/search" method="GET" role="search"> 
     {{ csrf_field() }} 
     <i class="fa fa-search"></i> 
     <div class="field-toggle"> 
      <input type="text" name="search" class="search-form" autocomplete="off" placeholder="Search..."> 
     </div> 
     <button type="submit" name="button">search</button> 
     </form> 
    </div> 

그리고이 제 기능입니다 :이 오류 받고 있어요 이것으로

public function search() { 

    $search = request('search'); 

    $foods = Food::join('food_ingredient', 'food_ingredient.food_id','=', 'food.id') 
     ->join('ingredients','ingredient.id','=','food_ingredient.ingredient.id') 
     ->where('food.title', 'LIKE', '%' . $search . '%') 
     ->orWhere('ingredient.title', 'LIKE', '%' . $search . '%') //The fix 
     ->orderBy('food.created_at', 'desc') 
     ->groupBy('food.id') 
     ->with('ingredients') 
     ->paginate(8); 

    return view('front.search', compact('foods')); 
} 

: 얼마나

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'food.title' in 'where clause' (SQL: select count(*) as aggregate from foods inner join food_ingredient on food_ingredient . food_id = food . id inner join ingredients on ingredient . id = food_ingredient . ingredient . id where food . title LIKE %papper% or ingredient . title LIKE %papper% group by food . id)

이것은 내 데이터베이스 :

Foods ->store posts

Ingredients ->Store ingredients

Food_ingredient ->store posts tags

12

어떻게 해결할 수 있습니까?

추신 : 내 게시물 이름은 food이고 내 태그 이름은 ingredient (다른 이름 지정)입니다.

감사합니다. 가정

$foods = Food::join('food_ingredient', 'food_ingredient.food_id','=', 'food.id') 
     ->join('ingredients','ingredient.id','=','food_ingredient.ingredient.id') 
     ->where('ingredient.title', 'LIKE', '%' . $search . '%') ... 

:

답변

0

사용 joins 후 여러 열을 가질 것이기 때문에 title을 요청 할 때 테이블 이름을 지정해야합니다, 그래서 당신은뿐만 아니라 where 절 테이블 이름을 지정해야 조인 당신은 성분 제목을 찾고 싶습니다

+0

당신은 'ingredtient.title' ... maybe' ingredients.name'로 검색하길 원한다고 당신이 말했듯이 당신의 필요에 맞게 where 절을 변경해야합니다. 이것은 당신이 무엇을 원하는지에 달려 있습니다. –

+0

'ingredtient.title'과'food.title' 내가 언급 한 부분을 제 잘못으로 변경했습니다. (여전히 오류가 발생하고 있습니다) – mafortis

+0

처음에는 2 개의 제목 (원래 질문에서도)을 기반으로 한 검색을 했으므로 테이블을 지정해야합니다. 당신이 필요로하는 것이거나 내가 구현하고자하는 논리를 추측 할 수 없다. 테이블 구조 나 Eloquent를 사용하여 생성하고자하는 SQL을 모르므로 –