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

时间:2017-05-19 19:17:46

标签: php laravel

我收到此错误:

Connection.php第647行中的ErrorException:SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'comments.dummy_id'(SQL:select * from comments其中commentsdummy_id = 1和commentsdummy_id不为空)(查看:C:\ xampp \ htdocs \ app \ resources \ views \ post.blade.php)

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

这是假人移民文件,本应该被称为帖子:

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

1 个答案:

答案 0 :(得分:0)

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

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