我正在尝试创建一个论坛,用户可以在其中发布线程,并且在线程底部,用户可以对线程发表评论,但是当我将评论部分添加到线程时,它会抛出SQLSTATE[42S02]
错误我正在尝试使用 Morph 关系从laravel https://laravel.com/docs/5.8/eloquent-relationships所以我可以将线程连接到相应的线程或评论。并且最终产品必须类似于 Reddits 一个http://prntscr.com/mwvors,其中评论彼此下方,评论可以评论其他评论。
编辑:在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
单刀.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);
}
网页.php
Route::resource('comment','CommentController', ['only' =>['update','destroy']]);
Route::post('comment/create/{thread}','ThreadController@storeComment')->name('threadcommment.store');