【问题标题】:Comment Table Giving SQLSTATE[42S02] laravel 5.8注释表给出 SQLSTATE[42S02] laravel 5.8
【发布时间】:2019-08-03 03:35:51
【问题描述】:

我正在尝试创建一个论坛,用户可以在其中发布线程,并且在线程底部,用户可以对线程发表评论,但是当我将评论部分添加到线程时,它会抛出 SQLSTATE[42S02] 我正在尝试的错误使用来自 laravel https://laravel.com/docs/5.8/eloquent-relationships 的 Morph 关系,所以我可以将线程连接到相应的线程或评论。并且最终的产品必须类似于 Reddits one http://prntscr.com/mwvors,其中评论相互放在一起,并且评论可以在其他 cmets 上发表。

编辑: 在php artisan migrate 之后,它更新了迁移但给出了这个错误

错误

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comments.commmentable_id' in 'where clause' (SQL: select * from `comments` where `comments`.`commmentable_id` = 1 and `comments`.`commmentable_id` is not null and `comments`.`commmentable_type` = App\Thread) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\thread\single.blade.php

single.blade.php

 {{--Answers/comments--}}
<div class="comment-list">
    @foreach($thread->comments as $comment)

        <h4>{{$comment->body}}</h4>
        <lead>{{$comment->user->name}}</lead>

    @endforeach
</div>
<div class="comment-form">
    <form action="{{ route('threadcomment.store', $thread->id) }}" method="post" role="form">
        {{csrf_field()}}
        <h4>Create Comment</h4>

        <div class="form-group">
            <input type="text" class="form-control" name="body" id="" placeholder="Input...">
        </div>

        <button type="submit" class="btn btn-primary">Comment</button>
    </form>
</div>

用户模型

 public function threads(){
    return $this->hasMany(Thread::class);
}

线程模型

 public function user()
{

    return $this->belongsTo(User::class);

}

public function comments()
{

    return $this->morphMany(Comment::class,'commmentable');

}

评论模型

 public function commenttable()
{

    return $this->morphTo();

}

public function user()
{

    return $this->belongsTo(User::class);

}

评论控制器

  public function addThreadComment(Request $request, Thread $thread)
{

    $this->validate($request,[
        'body' => 'required|min:10|max:250'
    ]);

    $comment = new Comment();
    $comment->body = $request->body;
    $comment->user_id = auth()->user()->id;

    $thread->comments()->save($comment);

}

web.php

 Route::resource('comment','CommentController', ['only' =>['update','destroy']]);
 Route::post('comment/create/{thread}','ThreadController@storeComment')->name('threadcommment.store');

【问题讨论】:

  • 您是否为 cmets 表创建了迁移并运行 php artisan migrate?听起来该表不存在。
  • 我确实忘记并更新了帖子,但它一直抛出 SQL 错误
  • 好的,那么现在表中是否存在列commmentable_id?您可能需要指定列名。 morphMany的函数定义是public function morphMany($related, $name, $type = null, $id = null, $localKey = null),所以可能需要传入id和localKey变量。
  • 将文档添加到一对一(多态)中,只要您有 $table-&gt;integer('commentable_id');$table-&gt;string('commentable_type');,您就不必通过任何东西,而且我在我的迁移中有这 2 个,因为它说我将带走他们两个Docs laravel 5.8

标签: php laravel eloquent laravel-5.8


【解决方案1】:

代码中有错别字

 public function comments()
 {
 return $this- >morphMany(Comment::class,'commmentable');
 }

  Route::post('comment/create/{thread}','ThreadController@storeComment')->name('threadcommment.store');

三倍 MMM 而不是 2 毫米

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 2018-04-30
    • 2017-04-19
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2018-01-15
    相关资源
    最近更新 更多