I am trying to make an forum where the user can post a Thread and on the bottom of the thread the user can comment to thread but when I add the commenting part to the thread it throws the SQLSTATE[42S02]
Error I am trying to use Morph relation ships from laravel https://laravel.com/docs/5.8/eloquent-relationships so I can connect the thread to the corresponding thread or comment. and the final product has to be someting like Reddits one http://prntscr.com/mwvors where comment go under each other and comment can be commented on other comments.
Edit: after php artisan migrate
it updated the the migrations but give this error instead
Error
"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>
user model
public function threads(){
return $this->hasMany(Thread::class);
}
thread model
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class,'commmentable');
}
comment model
public function commenttable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
comment controller
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');
there where typos in the code
public function comments()
{
return $this- >morphMany(Comment::class,'commmentable');
}
Route::post('comment/create/{thread}','ThreadController@storeComment')->name('threadcommment.store');
triple MMM instead of 2 mm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.