Laravel搜索表单不起作用

时间:2017-09-18 00:25:00

标签: php laravel laravel-5

我有一个搜索表单,它根据成分(标签的类型)进行搜索,而不是发布标题。

当我搜索时,我会收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'food_vaght.ingredient_id' in 'field list' (SQL: select `foods`.*, `food_vaght`.`ingredient_id` as `pivot_ingredient_id`, `food_vaght`.`food_id` as `pivot_food_id` from `foods` inner join `food_vaght` on `foods`.`id` = `food_vaght`.`food_id` where `food_vaght`.`ingredient_id` = 1) (View: C:\laragon\www\newproject\resources\views\front\search.blade.php)

这是我的搜索表单:

<form class="form-inline" action="/search" method="GET" role="search">
                          {{ csrf_field() }}
                          <div class="form-group">
                                    <label for="ingredients" class="hidden">Ingredient</label>
                                    <select class="tagsselector form-control" name="ingredients" multiple="multiple">
                                      @foreach($ingredients as $ingredient)
                                          <option value="{{ $ingredient->title }}">{{ $ingredient->title }}</option>
                                      @endforeach
                                    </select>
                          </div>

                                <div class="text-center">
                                    <button type="submit" class="mt-50 btn btn-custom">What to cook</button>
                                </div>

                       </form>

这是我的Search.blade.php,其结果将会返回:

@extends('layouts.web')

@section('title', 'Search')

@section('content')

<div class="row">
  <div class="col-md-8">
    searchbox
    <hr>

    <ol>
      @foreach($ingredients as $food)
        @foreach($food->foods as $res)
          <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
            <div class="card">
              <div class="card-block">
                <h4 class="card-title"><a href="#">{{$res->title}}</a></h4>
                <p class="card-text">{!!  str_limit($res->description, 10) !!}</p>
                <p><a class="btn btn-block btn-link" href="#">Read More <i class="fa fa-arrow-right"></i></a></p>
              </div>
            </div>
          </div>
        @endforeach
      @endforeach
    </ol>

  </div>
  <div class="col-md-4">
    random recipes
  </div>
</div>

@endsection

这是我的route

Route::any('/search', 'FrontendController@search');

这是FrontendController

中的搜索功能
public function search() {

      $searchTitle = request('title');

      $ingredients = null;

        $ingredients = Ingredient::when($searchTitle, function ($query) use ($searchTitle) {
            return $query->where('title', 'like', "%{$searchTitle}%");
          })
          ->paginate(10)
          ->appends(request()->query());

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

有什么想法吗?我甚至没有打电话给food_vaghtingredient_id,但为什么他们出现了我不理解的错误。

更新

Food_vaght迁移:

public function up()
    {
      Schema::create('food_vaght', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('food_id')->unsigned();
        $table->foreign('food_id')->references('id')->on('foods');
        $table->integer('vaght_id')->unsigned();
        $table->foreign('vaght_id')->references('id')->on('vaghts');
      });
    }

food_ingredient迁移:

public function up()
    {
      Schema::create('food_ingredient', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('food_id')->unsigned();
        $table->foreign('food_id')->references('id')->on('foods');
        $table->integer('ingredient_id')->unsigned();
        $table->foreign('ingredient_id')->references('id')->on('ingredients');
      });
    }

Foods迁移:

public function up()
    {
        Schema::create('foods', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title')->unique();
            $table->string('slug')->unique();
            $table->longText('description');
            $table->string('prepare')->nullable();
            $table->string('cook')->nullable();
            $table->integer('category_id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->string('image')->nullable();
            $table->string('status')->default(0);
            $table->timestamps();
        });

        Schema::table('foods', function($table) {
            $table->foreign('category_id')->references('id')->on('categories');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict')
                    ->onUpdate('restrict');
        });
    }

更新2:

食物模型

  public function ingredients(){
     return $this->belongsToMany(Ingredient::class);
  }
  public function vaghts(){
     return $this->belongsToMany(Vaght::class);
  }

Vaght模型:

  public function foods(){
     return $this->belongsToMany(Food::class);
  }

成分模型:

  public function foods(){
     return $this->belongsToMany(Food::class, 'food_vaght');
  }

1 个答案:

答案 0 :(得分:0)

<强>解决:

问题出在我的成分模型中

public function foods(){
     return $this->belongsToMany(Food::class, 'food_vaght');
  }

我把它改为:

public function foods(){
     return $this->belongsToMany(Food::class);
  }

感谢@MayurPadshala