繁体   English   中英

当我尝试显示每个帖子的评论时出现错误

[英]I get an error when I try to show the comments for each post

我收到此错误:

Connection.php第647行中的ErrorException:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ comments.dummy_id”(SQL:从comments中选择*,其中comments dummy_id = 1并且comments dummy_id不为null )(查看:C:\\ xampp \\ htdocs \\ app \\ resources \\ views \\ post.blade.php)

我有两张桌子,一张叫做假人,另一张叫做评论。 我知道它应该被称为邮政,而不是假人,我不好。 我在注释表上创建了一个外键,以链接到虚拟表中名为id的字段。

这是假人迁移文件,应称为post:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDummiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dummies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('dummies');
    }
}

这是评论迁移文件:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id');
            $table->string('name');
            $table->text('body');
            $table->timestamps();
            $table->foreign('post_id')->references('id')->on('dummies');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
}

让我知道你们是否需要用户或任何其他迁移文件。 我只显示了这两个,因为我认为这就是问题所在。 我很困惑为什么它给我一个甚至不存在的字段(是dummy_id字段)上的错误。

这是MattClark请求的我的post.blade.php文件:

<!DOCTYPE html>
<html>
  <head>

    <!--Import Google Icon Font-->
    <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!--Import materialize.css-->
    <link type="text/css" rel="stylesheet" href="../css/materialize.min.css"  media="screen,projection"/>
    <link type="text/css" rel="stylesheet" href="../css/main.css"  media="screen,projection"/>
      @yield('css')
    <!--Let browser know website is optimized for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>

  <body>
      @include('layout.nav')

      <main>
        <h3> {{ $post->name }}</h3>
        <p>{{ $post->created_at->toDayDateTimeString() }}</p>
        <p> {{ $post->body }}</p>
      </main>
      <ul>
        @foreach($post->comments as $comment)
        <li>{{ $comment->body }}</li>
        @endforeach
      </ul>
       @include('layout.footer')
    <!--Import jQuery before materialize.js-->
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="../js/materialize.min.js"></script>
      @yield('javascript')
  </body>
</html>

默认情况下,laravel假定您的外键是dummy_id 无论您将其命名为Dummy还是Post模型,都可以对其进行更改。

public function comments()
{
    return $this->hasMany('App\Comment', 'post_id');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM