aacassandra/laraquent

使用Laraquent节省您的宝贵时间,简化Laravel项目的CRUD操作

2.0.8 2020-11-27 15:34 UTC

This package is auto-updated.

Last update: 2024-09-28 00:03:16 UTC


README

这是一个专门为Laravel框架CRUD活动设计的包。此包使用Eloquent Laravel风格,并不改变默认的Eloquent风格。它只是使我们在不包含模型依赖的情况下执行CRUD操作更加容易。

支持功能

  • 创建
  • 读取
  • 更新
  • 删除
  • 用户
  • 创建用户
  • 更新用户
  • 删除用户
  • 用户列表

Laravel权限功能

如果您使用spatie/laravel-permission,则以下功能可以使用更简短的方法使用

  • 角色
  • 创建角色
  • 更新角色
  • 删除角色
  • 权限

Laravel中安装

此包可用于Laravel 5或更高版本。

  1. 请参阅“先决条件”页面,了解有关您的用户模型的重要注意事项!

  2. 此包会发布一个config/laraquent.php文件。如果您已经有了同名的文件,您必须重命名或删除它。

  3. 您可以通过composer安装此包

composer require aacassandra/laraquent
  1. 在config/app.php文件中的$providers数组中添加服务提供者
'providers' => [
    /*
    * Package Service Providers
    */
    // ...
    Laraquent\BeautyEloquentProvider::class,
];
  1. 将此包的外观添加到$aliases数组中。
'aliases' => [
    // ...
    'BeautyEloquent' => Laraquent\BeautyEloquent::class,
]
  1. 您必须发布配置,它将随后在config/laraquent.php配置文件中
php artisan vendor:publish --provider="Laraquent\BeautyEloquentProvider"
  1. 现在,Image类将由Laravel自动加载。

默认配置文件内容

您可以在以下位置查看默认配置文件内容:https://github.com/aacassandra/laraquent/resources/config/laraquent.php

基本用法

以下是使用Laravel的CRUD函数的一些基本示例

创建对象

格式

BeautyEloquent::Create($model = '', $data = []);

示例

$data = [
    ['name', 'Jhon Doe'],
    ['age', 33],
    ['address', 'Kampoeng Street']
];
$create = BeautyEloquent::Create('Product', $data);
dd($create); // Output

读取对象

格式

BeautyEloquent::Read($model = '', $options = []);

示例

$options = [
    'where' => [
        ['role','=','admin'],
        [...], //and more
    ],
    'orWhere' => [
        ['role','=','admin'],
        [...], //and more
    ],
    'whereJsonContains' => [
        ['slug->en', 'article-part-2'],
        [...], //and more
    ],
    'whereRaw' => [
        ['price > IF(state = "TX", ?, 100)', [200]],
        [...], //and more
    ],
    'orWhereRaw' => [
        ['price > IF(state = "TX", ?, 100)', [200]],
        [...], //and more
    ]
    'orderBy' => ['id', 'DESC'], //ASC or DESC
    'limit' => 2,
    'first' => true //if the result has a return value of 1 and no more and return json objects,
    'paginate' => [3, 'home'] //{x, y} : x => the amount of content to be displayed, y : pageName for multi pagination (optional)
];

$read = BeautyEloquent::Read('Product', $options);
dd($read); // Output

更新对象

格式

BeautyEloquent::Update($model = '', $data = [], $options = []);

示例

$data = [
    ['name', 'Jhon Doe'],
    ['age', 33],
    ['address', 'Kampoeng Street']
];

$options = [
    'where' => [
        ['status','=','sale'],
        [...], //and more
    ],
    'orwhere => [
        ['price','>=', 100],
        [...], //and more
    ],
    'id' => 3
];

$update = BeautyEloquent::Update('Product', $data, $options);
dd($update); // Output

删除对象

格式

BeautyEloquent::Delete($table = '', $options = []);

示例

$options = [
    'where' => [
        ['status','=','sale'],
        [...], //and more
    ],
    'orwhere => [
        ['price','>=', 100],
        [...], //and more
    ],
    'id' => 3
];

$delete = BeautyEloquent::Delete('Product', $options);
dd($delete); // Output

高级用法

以下是使用spatie/laravel-permission集成的一些高级示例(如有需要)

角色

显示所有角色

$options = [
    //Same as with the conditionals in the Read Object function
];
$roles = BeautyEloquent::Roles($options)
dd($roles); // Output

创建角色

$name = 'Admin';
$permissions = ['1','2','3']; // id of permission, you can use string or number format
$createRole = BeautyEloquent::CreateRole($name, $permissions)
dd($createRole); // Output

更新角色

$id = 1;
$name = 'New Admin';
$permissions = ['1','2','3']; // id of permission, you can use string or number format
$updateRole = BeautyEloquent::UpdateRole($id, $name, $permissions)
dd($updateRole); // Output

删除角色

$id = 1;
$deleteRole = BeautyEloquent::DeleteRole($id)
dd($deleteRole); // Output

权限

显示所有权限

$options = [
    //Same as with the conditionals in the Read Object function
];
$permissions = BeautyEloquent::Permissions($options)
dd($permissions); // Output

用户

通过id获取特定用户

$id = 1;
$user = BeautyEloquent::User($id)
dd($user); // Output

用户列表

获取所有用户

$options = [
    //Same as with the conditionals in the Read Object function
];
$users = BeautyEloquent::Users($options)
dd($users); // Output

创建用户

$data = [
    ['name', 'Jhon Doe'],
    ['age', 33],
    ['address', 'Kampoeng Street'],
    [...], //and more
];
$roles = ['Admin', 'Super Admin', ...]; // string of role name
$createUser = BeautyEloquent::CreateUser($data, $roles)
dd($createUser); // Output

更新用户

$id = 1;
$data = [
    ['name', 'Jhon Doe'],
    ['age', 33],
    ['address', 'Kampoeng Street'],
    [...], //and more
];
$roles = ['Admin', 'Super Admin', ...]; // string of role name
$updateUser = BeautyEloquent::UpdateUser($id, $data, $roles)
dd($updateUser); // Output

删除用户

$id = 1;
$deleteUser = BeautyEloquent::DeleteUser($id)
dd($deleteUser); // Output

包配置必须配置

如果使用Laravel 8,必须配置配置文件,因为Laravel 8中文件模型已移动到Models目录

    'model' => [
        /**
         * The default directory of models
         */
        'directory' => 'App\Models',
        /**
         * Default of User Model
         */
        'user' => 'App\Models\User',
        /**
         * If using [spatie/laravel-permission]
         * you must set default model off this
         */
        'role' => 'Spatie\Permission\Models\Role',
        'permission' => 'Spatie\Permission\Models\Permission'
    ],

请确保始终更新有关角色模型和权限的信息。您可以在spatie/laravel-permission的官方网站上查看它们

许可

此项目受MIT许可协议的许可 - 有关详细信息,请参阅LICENSE.md文件