方法 Livewire\Redirector::withInput 不存在。在 laravel

Method Livewire\Redirector::withInput does not exist. in laravel

我正在使用火线,当我尝试验证表格时它说 我正在使用火线,当我尝试验证它说的表格时 我正在使用火线,当我尝试验证它说的表格时 我正在使用带电电线,当我尝试验证表格时它说

Method Livewire\Redirector::withInput does not exist.

这是我的代码

Posts.php
<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Post;

class Posts extends Component
{
    public $title;
    public $content;

    public function hydrate(){
        $this->validate([
            'title' => 'required',
            'content' => 'required'
        ]);
    }

    public function save(){
        $data = [
            'title' => $this->title,
            'content' => $this->content,
            'user_id' => Auth()->user()->id
        ];

        Post::create($data);
        $this->cleanVars();
    }

    private function cleanVars(){
        $this->title = null;
        $this->content = null;
    }

    public function render()
    {
        return view('livewire.posts');
    }
}

livewire 视图

<div>
    <label>Title</label>
    <input wire:model="title" type="text" class="form-control" />
    @error('title')
        <p class="text-danger">{{ $message }}</p>
    @enderror
    <label>Content</label>
    <textarea wire:model="content" type="text" class="form-control"></textarea>
    @error('content')
    <p class="text-danger">{{ $message }}</p>
    @enderror
    <br />
    <button wire:click="save" class="btn btn-primary">Save</button>
</div>

我也把这个视图放在 home.blade.php

    @extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Dashboard') }}</div>

                <div class="card-body">
                    @livewire('posts')
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

您真的需要解决此问题的 header,重复同样的问题,您在社区中遗漏了一些东西。我在你的代码中看到了这一点,但我不喜欢这种用法

public function hydrate(){
  $this->validate([
     'title' => 'required',
     'content' => 'required'
  ]);
}

我的意思是,这种对每次水合作用的验证 运行 并不是一个好的方法。相反,声明规则

protected $rules = [// rules here];

//or

public function rules()
{
   return [
     //rules here
   ];
}

然后您可以验证条目,例如 real-time 使用 updated()

中的 validateOnly 方法进行验证
public function updated($propertyName)
{
  $this->validateOnly($propertyName, $this->rules());
}

或者直接在保存方法中使用

public function save()
{
   $this->validate();  // in the protected $rules property
    // or
   Post::create($this->validate());
}