返回数据透视表数据以在Laravel中查看

时间:2018-06-02 19:54:09

标签: php ajax laravel

我不太清楚我做错了什么......

我有一个我正在学习的食谱项目,但我似乎无法通过数据透视表将数据加入到视图中。

用户创建配方[在(名称,描述,部分,标签等),然后定向到“创建成分”视图。从这里开始,他们通过Ajax请求添加成分,成功保存到成分表,以及 recipe_ingredients 中的链接。

我通过ajax动态地将成分添加到表中,但是当用户刷新页面时,我需要它遍历成分表,并返回与给定配方相关的那些。

我正在使用以下循环,但它不起作用:

@foreach($recipe->ingredients as $ingredient)
  {{$recipe->ingredient->name}}
@endforeach

我收到以下错误:

ErrorException (E_ERROR):
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ingredients.recipe_ingredients' in 'where clause' (SQL: select * from `ingredients` where `ingredients`.`recipe_ingredients` is null and `ingredients`.`recipe_ingredients` is not null) (View: C:\xampp\htdocs\sites\recipes\resources\views\ingredients\create.blade.php)

信息:

我有3张桌子: 食谱成分 recipe_ingredients

食谱型号:

class Recipe extends Model
{
  public function ingredients(){
    return $this->hasMany('App\Ingredient', 'recipe_ingredients', 'recipe_id', 'ingredient_id');
  }

成分型号:

class Ingredient extends Model
{
    public function recipes(){
    return $this->belongsToMany('App\Recipe', 'recipe_ingredients', 'ingredient_id', 'recipe_id');
}

成分控制器

public function create($recipe_id)
{
  $recipe = Recipe::find($recipe_id);
  return view('ingredients.create')->withRecipe($recipe);
}

public function store(Request $request)
{

  $this->validate($request, [
    'name' => 'required|max:255'
  ]);

  $ingredient = new Ingredient;
  $ingredient->name = $request->name;
  $ingredient->save();

  $recipe = Recipe::find($request->id);
  $ingredient->recipes()->attach($recipe->id);

  $data = [
    'success' => true,
    'message'=> 'Your AJAX processed correctly',
    'name' => $ingredient->name,
    'recipe' => $recipe,
  ] ;

    return response()->json($data);
}

用于将成分保存到db的Ajax脚本:

<script>
$(document).ready(function(){
  $("#submit").click(function() {
    var name = $("#ingredientName").val();
    var token = $("#token").val();

    $.ajax({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
      type: "post",
      data: "name="+name,
      dataType:'json',
      url: "{{ route('ingredients.store', ['id' => $recipe->id]) }}",
      success:function(data){
        console.log(data);
        $("#msg").html('<div class="alert alert-success my-0">'+data.name+' added</div>');
        $("#msg").toggleClass("invisible")
        $("#msg").fadeOut(2000);
        $("#ingredientsTable").append('<tr><td scope="col" class="align-middle">'+data.name+'</td></tr>');
      }
    });
  })
})
</script>

观点的相关部分:

<div class="row">
  <div class="col-lg-3">
    <div class="card">
      <div class="card-body">
        <!-- 
        <p><b>Est. prep time:</b></p>
        <p><b>Est. cooking time:</b></p>
        -->
        <p><b>Portions:</b> {{ $recipe->portions }}</p>
        <p><b>Creator:</b> <a href="#">{{ $recipe->user->name }}</a></p>
        @if($recipe->created_at == $recipe->updated_at) 
          <p><b>Created:</b> {{ $recipe->created_at->diffForHumans() }}</p>
        @else
          <p><b>Updated:</b> {{ $recipe->updated_at->diffForHumans() }}</p>
        @endif
        <!-- 
        <p><b>Rating:</b> <i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star-half"></i>&nbsp;(52)</p>
        -->

        <div id="tags">
          @foreach($recipe->tags as $tag)
            <button type="button" class="btn btn-outline-{{$tag->colour}} btn-sm my-1">{{$tag->name}}</button>
          @endforeach
        </div>

      </div>
    </div>
  </div>

  <div class="col-lg-9">
    <table class="table table-hover table-light" style="border: 1px solid rgba(0,0,0,.125);">
      <thead>
        <tr>
          <th scope="col">Ingredients:</th>
        </tr>
      </thead>
      <tbody id="ingredientsTable">

        @foreach($recipe->ingredients as $ingredient)
        {{$recipe->ingredient->name}}
        @endforeach
      </tbody>
    </table>
  </div>
</div>

路线:

$ php artisan route:list
+--------+-----------+--------------------------+--------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                      | Name               | Action                                                                 | Middleware   |
+--------+-----------+--------------------------+--------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                        |                    | App\Http\Controllers\PagesController@index                             | web          |
|        | GET|HEAD  | api/user                 |                    | Closure                                                                | api,auth:api |
|        | GET|HEAD  | home                     | home               | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | GET|HEAD  | login                    | login              | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | login                    |                    | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | POST      | logout                   | logout             | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST      | password/email           | password.email     | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | GET|HEAD  | password/reset           | password.request   | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | POST      | password/reset           |                    | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset/{token}   | password.reset     | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST      | recipes                  | recipes.store      | App\Http\Controllers\RecipesController@store                           | web,auth     |
|        | GET|HEAD  | recipes                  | recipes.index      | App\Http\Controllers\RecipesController@index                           | web,auth     |
|        | GET|HEAD  | recipes/create           | recipes.create     | App\Http\Controllers\RecipesController@create                          | web,auth     |
|        | GET|HEAD  | recipes/{id}/ingredients | ingredients.create | App\Http\Controllers\IngredientsController@create                      | web,auth     |
|        | POST      | recipes/{id}/ingredients | ingredients.store  | App\Http\Controllers\IngredientsController@store                       | web,auth     |
|        | GET|HEAD  | recipes/{recipe}         | recipes.show       | App\Http\Controllers\RecipesController@show                            | web,auth     |
|        | DELETE    | recipes/{recipe}         | recipes.destroy    | App\Http\Controllers\RecipesController@destroy                         | web,auth     |
|        | PUT|PATCH | recipes/{recipe}         | recipes.update     | App\Http\Controllers\RecipesController@update                          | web,auth     |
|        | GET|HEAD  | recipes/{recipe}/edit    | recipes.edit       | App\Http\Controllers\RecipesController@edit                            | web,auth     |
|        | POST      | register                 |                    | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | register                 | register           | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
|        | POST      | steps                    | steps.store        | App\Http\Controllers\StepsController@store                             | web,auth     |
|        | GET|HEAD  | steps                    | steps.index        | App\Http\Controllers\StepsController@index                             | web,auth     |
|        | GET|HEAD  | steps/create             | steps.create       | App\Http\Controllers\StepsController@create                            | web,auth     |
|        | GET|HEAD  | steps/{step}             | steps.show         | App\Http\Controllers\StepsController@show                              | web,auth     |

迁移

create_recipes_table

Schema::create('recipes', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->text('description');
  $table->integer('portions');
  $table->integer('user_id');
  $table->timestamps();
});

create_ingredients_table

Schema::create('ingredients', function (Blueprint $table) {
    $table->increments('id');
    $table->text('name');
    $table->boolean('approved')->nullable();
    $table->datetime('approved_at')->nullable();
    $table->integer('approved_by')->nullable();
    $table->timestamps();
});

create_recipe_ingredients_table

Schema::create('recipe_ingredients', function (Blueprint $table) {
    $table->integer('recipe_id')->unsigned();
    $table->foreign('recipe_id')->references('id')->on('recipes');

    $table->integer('ingredient_id')->unsigned();
    $table->foreign('ingredient_id')->references('id')->on('ingredients');
});

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为您的食谱模型上的关系不正确。由于食谱和igredients之间的关系是多对多的,因此两个模型都需要与另一个模型有belongsToMany的关系:

class Recipe extends Model
{
    public function ingredients(){
        return $this->belongsToMany('App\Ingredient', 'recipe_ingredients');
    }
}

foreach中也有错误,您应该从循环中引用$ingredient

@foreach($recipe->ingredients as $ingredient)
  {{ $ingredient->name }}
@endforeach