Home > Mobile >  laravel 9 bug when writing
laravel 9 bug when writing

Time:09-11

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 >
        <form action="{{ route('thread.store',$subcategory) }}" method="POST">
            @csrf
            <div >
                <div >
                    <div >
                        <div >
                            <h4 > Post Thread </h4>
                        </div>

                        <div >
                            <div >
                                <label> Title </label>
                                <input type="text"  name="title" placeholder="Enter the Title" required>
                            </div>
                            <div  >
                                <label> Description </label>
                                <textarea  id="description" placeholder="Enter the Description" name="description"></textarea>
                            </div>
                        </div>

                        <div >
                            <button type="submit" > 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.

CodePudding user response:

You are mixing standard HTML forms with Livewire. If you're going to use Livewire, use it for everything. When you submit your form it's "jumping" to the top of the page because its not respecting the Livewire component, it's just treating it like a static page.

For your form, use wire:submit.prevent="myAction" and then add myAction function to your Livewire component. Stop mixing static, oldschool forms when you can use the superior Livewire equivalent.

CodePudding user response:

The SqlState error cannot be null come because you didnt put description column as nullable like this

$this->text('description')->nullable();

and like this you can send empty value of descrption without probleme

  • Related