I wrote a function to create comments without reloading the page, but when filling in the field, the console gives the following error:
I used last version Livewire v1.0.9
And my code:
Controller:
public $news_id;
public $comment_text;
public $edited_com_text;
protected $comments;
public function render(){
echo $this->news_id.' text-'.$this->comment_text.'-text '.Auth::id();
$this->comments = \App\Comments_news::
join('users', 'comments_news.user_id', '=', 'users.id')
->join('news', 'comments_news.news_id', '=', 'news.id')
->select('comments_news.txt','comments_news.created_at','users.name','users.img','comments_news.id','comments_news.user_id')
->where('comments_news.news_id','=',$this->news_id)
->orderByDesc('comments_news.id')
->paginate(5);
return view('livewire.news-comments',['comments' => $this->comments]);
}
public function mount($news){
$this->news_id = $news->id;
}
public function addComment(){
\App\Comments_news::create([
'txt' => $this->comment_text,
'user_id' => Auth::id(),
'news_id' => $this->news_id,
]);
$this->comment_text = '';
$rank = \App\User::select('rank')->where('id', '=', Auth::id())->first();
$rank->rank += 1;
\App\User::where('id', '=', Auth::id())->update(['users.rank' => $rank->rank]);
}
Block with input and button:
<div class="row d-flex justify-content-center">
<div class="col-md-8 col-lg-8 col-12">
<input type="text" style="width: 100%;" wire:model="comment_text">
</div>
</div>
<div class="row">
<div class="col d-flex d-sm-flex d-xl-flex justify-content-end" style="margin: 10px 0 15px 0;">
<button class="btn btn-dark text-white d-xl-flex" wire:click="addComment" type="submit">Добавить комментарий</button>
</div>
</div>
And calling livewire:
@livewire(‘news-comments’,[‘news’=>$news])