【问题标题】:post request not working in laravel livewire发布请求在 laravel livewire 中不起作用
【发布时间】:2021-11-17 16:32:42
【问题描述】:

大家好,我是 laravel 和 livewire 的新手,请协助,发布请求没有通过,如果我点击提交没有任何反应,我也没有收到错误,我已经在我的应用程序中添加了 livewire 脚本.blade.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');
    }
}

【问题讨论】:

  • 您的wire:model 指向post.title,但是,您的验证仅针对titlebody 也是如此。
  • 我已经改了,但还是不行
  • 您是否检查过是否加载了 livewire 资产?
  • 是的,我检查过
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: php laravel laravel-8 laravel-livewire


【解决方案1】:

lazy 添加到the 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>

【讨论】:

  • 为什么会突然起作用?
【解决方案2】:

这里有两点需要注意 - 你没有初始化 $this-&gt;post 并且你没有正确的验证。您需要检查post.titlepost.body,还需要显示实际错误。

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

要显示验证错误,您可以在刀片文件中添加以下 sn-p,

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

【讨论】:

    猜你喜欢
    • 2018-10-06
    • 2015-11-13
    • 2019-01-27
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    相关资源
    最近更新 更多