php - 发布请求在 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');
}
}
解决方案
添加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>
推荐阅读
- reactjs - 在 Netify 中从 package.json 运行两个脚本
- c# - 在我的 C# 应用程序中:如何使用 AT 命令重建从 GSM 调制解调器获取的文本消息,以显示表情符号/表情符号以及单词?
- javascript - 如何从字符串中创建具有数组属性的对象?
- pygame - 哪些 pygame 模块具有 init 属性?我想手动初始化模块而不是使用 pygame.init()
- mysql - MySql 日期时间格式更改
- python - Command errored out with exit status 1: Building wheel for pysph (setup.py) ... error
- ffmpeg - 使用 ffmpeg 将 heif 文件转换为 png/jpg
- elasticsearch - Kibana painless script to count the json list size
- javascript - Palindrome Index Hackerrank with JavaScript
- jobs - 测试数据生成的作业可以在 HCL OneTest Data 中保留多长时间?用户可以为作业配置此时间段吗?