Search code examples
phplaravellaravel-8laravel-livewire

post request not working in laravel livewire


hello guys I'm new to laravel and livewire please kindly assist, post request not going through , if i click on the submit nothing is happening, I'm not getting error either, I have added the livewire script in my app.blade.php and it's rendering properly

Post Create form

<div>
    <div class="p-4 mx-auto mt-3 bg-gray-100 md:p-8 md:w-4/5 md:mt-0">
        <h1 class="mb-3 text-xl font-semibold text-gray-600">New post</h1>
        <form wire:submit.prevent="createPost" action="#" class="px-4 py-6 space-y-4">
            <div class="overflow-hidden bg-white rounded-md shadow">
                <div class="px-4 py-3 space-y-8 sm:p-6">
                    <div class="grid grid-cols-6 gap-6">
                        <div class="col-span-6 sm:col-span-3">
                            <input class="w-full" type="text"
                               wire:model="post.title" placeholder="Post title" />
                        </div>

                    </div>

                    <div class="flex flex-col">
                        <textarea id="body" rows="4" wire:model="post.body"
                           class="border-gray-300 rounded-sm form-textarea">
                        </textarea>
                    </div>
                </div>
                <div class="px-4 py-3 text-right bg-gray-50 sm:px-6">
                    <button type="submit" class="inline-flex justify-center">
                        post
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

this is my post create livewire method

<?php

namespace App\Http\Livewire;

use App\Models\Post;
use Livewire\Component;
use Illuminate\Http\Response;

class PostCreate extends Component
{
    public $post;
    public $points = 10;
    public $energy = 1;

    public function increment()
    {
        $this->points++;
    }

    protected $rules = [
        // 'category' => 'required|integer|exists:categories,id',
        'title' => 'required|min:4',
        'body' => 'required|min:4',
    ];

    public function createPost()
    {
        if (auth()->check()) {
            $this->validate();

            $post = Post::create([
                'user_id' => auth()->user()->id,
                // 'category_id' => $this->category,
                'body' => $this->body,
                'title' => $this->title,

            ]);


            $users = auth()->user();
            $users->increment('points', 10);

            session()->flash('success_message', 'Post was added successfully!');

            $this->reset();

            return redirect()->route('posts.index');
        }

        // abort(Response::HTTP_FORBIDDEN);
    }

    public function render()
    {
        return view('livewire.post-create');
    }
}

Solution

  • There are two thing here to note - you don't initialize $this->post and you don't have the proper validation. You need to check for post.title and post.body, and you also need to display the actual errors.

    <?php
    
    namespace App\Http\Livewire;
    
    use App\Models\Post;
    use Livewire\Component;
    
    class PostCreate extends Component
    {
        public $post;
        public $points = 10;
        public $energy = 1;
    
        public function mount()
        {
            $this->post = new Post;
            $this->post->user_id = auth()->id();
        }
    
        public function increment()
        {
            $this->points++;
        }
    
        protected $rules = [
            // 'category' => 'required|integer|exists:categories,id',
            'post.title' => 'required|min:4',
            'post.body' => 'required|min:4',
        ];
    
        public function createPost()
        {
            if (auth()->check()) {
                $this->validate();
    
                $post = $this->post->save();
    
                auth()
                    ->user()
                    ->increment('points', 10);
    
                session()->flash('success_message', 'Post was added successfully!');
    
                $this->reset();
    
                return redirect()->route('posts.index');
            }
    
            // abort(Response::HTTP_FORBIDDEN);
        }
    
        public function render()
        {
            return view('livewire.post-create');
        }
    }
    

    To display the errors from validation, you can add the following snippet in your blade-file,

    @if ($errors->any())
        <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
        </ul>
    @endif