milenmk / laravel-livewire-crud
可用于所有 Laravel 控制器以及/或 Livewire 组件的 CRUD 文件
1.0.1
2024-01-21 13:25 UTC
Requires
- php: ^8.2
Requires (Dev)
- orchestra/testbench: ^8.21
This package is auto-updated.
Last update: 2024-09-21 14:47:23 UTC
README
此包处理您为 Laravel 控制器或 Livewire 组件可能需要的所有 CRUD 操作的逻辑
要求
最小 PHP 版本:8.2
安装
- 运行
composer require milenmk/laravel-livewire-crud
来安装包
使用方法
- 创建一个新的Trait,例如命名为 GetSetData,并在开头插入
use CrudClass;
- GetSetData 文件将用于在创建或编辑时设置属性的值。您可以在
src/GetSetData.php
中查看示例文件 - 在控制器/组件中,在开头插入
use GetSetData;
- 将 crud 方法引用到 CrudClass 中相应的方法,并将模型名称作为参数传递。例如,如果您有一个
Client
组件和一个Client
模型,Livewire 组件可能看起来像这样
<?php
declare(strict_types=1);
namespace App\Livewire\Client;
use App\Livewire\CommonComponent;
use App\Models\Client;
use App\Traits\GetSetData;
final class Clients extends CommonComponent
{
use GetSetData;
public function storeClient(): void
{
$this->rules = [
'company' => 'required|min:3',
'country' => 'nullable',
'city' => 'nullable',
'zip' => 'nullable',
'address' => 'nullable',
'phone' => 'nullable',
'fax' => 'nullable',
'mobile' => 'nullable',
'email' => 'nullable',
];
$this->commonStoreData('Client');
}
public function editClient(int $clientId): void
{
$this->commonEditData('Client', $clientId);
}
public function updateClient(): void
{
$this->rules = [
'company' => 'required|min:3',
'country' => 'nullable',
'city' => 'nullable',
'zip' => 'nullable',
'address' => 'nullable',
'phone' => 'nullable',
'fax' => 'nullable',
'mobile' => 'nullable',
'email' => 'nullable',
'status' => 'required',
];
$this->commonUpdateData('Client');
}
public function deleteClient(int $clientId): void
{
$this->commonDeleteData('Client', $clientId);
}
public function destroyClient(): void
{
$this->commonDestroyData('Client');
}
public function bulkDestroyClients(): void
{
$this->commonBulkDestroyData('Client');
}
}
- 要使用批量删除选项,您必须在组件中有一个名为
selectedItems
的属性
/**
* Array of selected items for bulk action
*
*/
public array $selectedItems = [];