I have 3 small problems in my code
Livewire ThreadDisplay.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Thread;
use App\Models\User;
use App\Models\ThreadReply;
use Auth;
class ThreadDisplay extends Component
{
public Thread $thread;
public ThreadReply $threadreply;
public $description;
protected $listeners = ['update-thread' => 'updatethread'];
public function mount(Thread $thread)
{
$this->thread = $thread;
// dd($this->thread);
}
public function react($reaction)
{
if ($this->thread->isLiked()) {
$this->thread->toggleReaction($reaction);
if($reaction == $this->thread->reacted()?->type){
if($reaction == 'hand-thumbs-down-fill' || $reaction == 'emoji-frown-fill' ){
$user = User::find($this->thread->user->id)->increment('reactions', 1);
}else{
$user = User::find($this->thread->user->id)->decrement('reactions', 1);
}
}else{
$negative = ['hand-thumbs-down-fill','emoji-frown-fill'];
$positive = ['hand-thumbs-up-fill','emoji-heart-eyes-fill','heart-fil'];
if(in_array($reaction,$negative)){
if(!in_array($this->thread->reacted()->type,$negative)){
$user = User::find($this->thread->user->id)->decrement('reactions', 2);
}
}else{
if(!in_array($this->thread->reacted()->type,$positive)){
$user = User::find($this->thread->user->id)->increment('reactions', 1);
}
}
}
} else{
$this->thread->toggleReaction($reaction);
if($reaction == 'hand-thumbs-down-fill' || $reaction == 'emoji-frown-fill' ){
$user = User::find($this->thread->user->id)->decrement('reactions', 1);
}else{
$user = User::find($this->thread->user->id)->increment('reactions', 1);
}
}
$this->thread = Thread::find($this->thread->id);
}
public function replysubmit(){
$threadreply = new ThreadReply();
$threadreply->thread_id = $this->thread->id;
$threadreply->user_id = Auth::user()->id;
$threadreply->description = $this->description;
$this->description = '';
$threadreply->save();
$this->dispatchBrowserEvent('threadreplysent');
}
public function updatethread(){
$this->thread = Thread::find($this->thread->id);
}
public function render()
{
return view('livewire.thread-display');
}
}
Livewire ThreadReply.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\ThreadReply as TR;
use App\Models\Thread;
use App\Models\User;
use Auth;
class ThreadReply extends Component
{
public TR $reply;
public function mount(TR $reply)
{
$this->reply = $reply;
}
public function react($reaction)
{
if ($this->reply->isLiked()) {
$this->reply->toggleReaction($reaction);
if($reaction == $this->reply->reacted()?->type){
if($reaction == 'hand-thumbs-down-fill' || $reaction == 'emoji-frown-fill' ){
$user = User::find($this->reply->user->id)->increment('reactions', 1);
}else{
$user = User::find($this->reply->user->id)->decrement('reactions', 1);
}
}else{
$negative = ['hand-thumbs-down-fill','emoji-frown-fill'];
$positive = ['hand-thumbs-up-fill','emoji-heart-eyes-fill','heart-fil'];
if(in_array($reaction,$negative)){
if(!in_array($this->reply->reacted()->type,$negative)){
$user = User::find($this->reply->user->id)->decrement('reactions', 2);
}
}else{
if(!in_array($this->reply->reacted()->type,$positive)){
$user = User::find($this->reply->user->id)->increment('reactions', 1);
}
}
}
} else{
$this->reply->toggleReaction($reaction);
if($reaction == 'hand-thumbs-down-fill' || $reaction == 'emoji-frown-fill' ){
$user = User::find($this->reply->user->id)->decrement('reactions', 1);
}else{
$user = User::find($this->reply->user->id)->increment('reactions', 1);
}
}
$this->reply = TR::find($this->reply->id);
$this->dispatchBrowserEvent('replyreaction');
}
public function render()
{
return view('livewire.thread-reply');
}
}
ThreadController
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\Thread;
use App\Models\ThreadReply;
use App\Models\SubCategory;
use Auth;
class ThreadController extends Controller
{
public function create($subcategory){
return view('threads.create',compact('subcategory'));
}
public function store(Request $request,$subcategory){
$category = SubCategory::where('title',$subcategory)->first();
$thread = new Thread();
$thread->title = $request->title;
$thread->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->title)).'-'.Str::random(5);
$thread->description = $request->description;
$thread->sub_category_id = $category->id;
$thread->user_id = Auth::user()->id;
$thread->save();
return redirect()->route('subcategory.thread',$request->subcategory);
}
public function show($slug){
$thread = Thread::where('slug',$slug)->first();
return view('threads.show',compact('thread'));
}
}
create thread
@extends('layouts.app')
@section('stylesheets')
{{-- <link href="{{ asset('ckeditor/contents.css') }}" rel="stylesheet"> --}}
@endsection
@section('content')
<div class="container-fluid mt-5">
<form action="{{ route('thread.store',$subcategory) }}" method="POST">
@csrf
<div class="row">
<div class="col-xl-12 col-lg-12 col-sm-12 col-12 m-auto">
<div class="card shadow">
<div class="card-header">
<h4 class="card-title"> Post Thread </h4>
</div>
<div class="card-body">
<div class="form-group">
<label> Title </label>
<input type="text" class="form-control" name="title" placeholder="Enter the Title" required>
</div>
<div class="form-group" >
<label> Description </label>
<textarea class="form-control" id="description" placeholder="Enter the Description" name="description"></textarea>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success"> Save </button>
</div>
</div>
</div>
</div>
</form>
</div>
@endsection
@section('scripts')
<script src="//cdn.ckeditor.com/4.19.0/standard/ckeditor.js"></script>
<script>
// Replace the <textarea id="editor1"> with a CKEditor 4
// instance, using default configuration.
CKEDITOR.replace( 'description' );
</script>
@endsection
The first bug I have; when I try to write in reply thread, it takes me to the top of the page and I have to click back to continue writing.
My other problem is when I create a thread and the description is empty I get this error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null
I would like that instead of the error that you see, I would like it to write to provide me with an error description.
requiredrule.