首页 > 解决方案 > 发布请求在 laravel livewire 中不起作用

问题描述

大家好,我是 laravel 和 livewire 的新手,请协助,发布请求未通过,如果我单击提交没有任何反应,我也没有收到错误,我已在 app.blade 中添加了 livewire 脚本。 php 并且它正在正确呈现

发布创建表单

<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>

这是我的帖子创建 livewire 方法

<?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');
    }
}

标签: phplaravellaravel-8laravel-livewire

解决方案


添加lazythe wire.model

<input class="w-full" type="text" wire:model.lazy="post.title" placeholder="Post title" />
<textarea id="body" rows="4" wire:model.lazy="post.body" class="border-gray-300 rounded-sm form-textarea"></textarea>

推荐阅读