laramate / flex-properties
Laravel Flex 属性
Requires
- php: ^7.1.3
- illuminate/database: >=5.7.0
- illuminate/support: >=5.7.0
- mindtwo/laravel-dynamic-model-mutators: ^2.0.1
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-09-06 11:54:29 UTC
README
简介
使用本包,您可以定义 Laravel Eloquent 模型的自定义属性。
安装
您可以通过 composer 安装此包
composer require tkivelip/laravel-flex-properties
如何使用?
设置模型
简单设置
使用此包最简单的方法是扩展已提供的模型。您需要的任何东西都已经设置好了。现在您可以通过创建一个名为 'flex_properties' 的属性来自定义 flex 属性;
namespace Example; use tkivelip\LaravelFlexProperties\Model; class ExampleModel extends Model { protected $flex_properties = [ 'title' => 'string', 'text' => 'text', 'data' => 'json', ]; }
$flex_properties 键是您属性的名称。作为值,您必须设置一个有效的 flex 属性类型。您可以在主配置文件 config/flex-properties.php 中添加或更改 flex 属性类型。
发布配置和迁移
如果您想自定义配置或迁移,可以将这些文件发布到您的本地安装。老套路。使用任何终端,进入您的项目根目录,然后输入
php artisan vendor:publish
扩展设置
即将推出...
使用 Flex 属性
Flex 属性与所有常用的 Eloquent 函数一起工作,如 fill()、update()、create() 等。它们有自己的基于类型的数据库表,因此如果您更改 flex 属性配置,则不需要更新迁移。
设置值
在设置好您的 eloquent 模型配置后,您可以像设置其他模型属性一样设置 flex 属性。以下是一些示例
namespace Example; // You can use make() to fill flex properties $model = ExampleModel::make([ 'title' => 'Example', 'description' => 'Look at this fill() example' ]); // Or set a property directly $model->title = 'Overrides example'; // Or use fill() on an existing object $model->fill([ 'title' => 'Example', 'description' => 'Look at this fill() example' ]); // Don't forget to save you changes ExampleModel->save();
注意:您必须配置
$fillable和/或$guarded属性以允许批量赋值。有关详细信息,请参阅 Laravel 的 批量赋值文档。
获取值
您可以像访问其他 Eloquent 属性一样直接访问值。
namespace Example;
$model = ExampleModel::create([
'title' => 'Example',
'description' => 'Look at this fill() example'
]);
echo $model->title;
echo $model->description;
追加 Flex 属性
您还可以使用 Eloquent 的 append 函数自动将 flex 属性追加到您的模型。
namespace Example; use tkivelip\LaravelFlexProperties\Model; class ExampleModel extends Model { protected $flex_properties = [ 'title' => 'string', 'text' => 'text', ]; protected $appends = [ 'title', 'text', ]; }
创建和更新模型
创建和更新模型非常简单。只需使用常规的 Eloquent 方法。
namespace Example; $example = ExampleModel::create([ 'title' => 'Example', 'description' => 'Look at this fill() example' ]); $example->update([ 'title' => 'New title', 'description' => 'A more senceless description' ]);
本地化
Flex 属性具有实现本地化机制。因此,您可以以任何您喜欢的语言设置、获取和查询 flex 属性。只需使用 locale() 方法在任何时候更改区域设置。
重要提示:
locale()方法仅更改 flex 属性的区域设置。这不会更改您应用程序的环境。
// Set the english title $model->locale('en')->title = 'Example'; / / Set the german title $model->locale('de')->title = 'Beispiel'; // Or fill frensh translations $model->locale('fr)->fill([ 'title' => 'Exemple', 'description' => 'Parlez-vous français?' ]);
注意:如果您没有设置区域设置,则使用默认区域设置。如果您
手动更改了区域设置,它将在当前生命周期中保存到模型,但不会在持久化层中保存。
Flex 辅助类
您可以在 Flex 辅助类中找到一些有用的工具。例如,我们可以使用 typeExists() 方法来确定是否配置了 flex 属性类型。
namespace Example; use tkivelip\LaravelFlexProperties\Flex; Flex::typeExists('string'); // returns true
查询 Flex 属性
您可以使用 Flex::where() 辅助函数来扩展常规查询构建器函数,如 where()、orWhere()、AndWhere() 等。它接受 flex 属性名称作为第一个参数。所有其他参数与 eloquent 构建器方法相同。
namespace Example; ExampleModel::where( Flex::where('property_name', $value); ); ExampleModel::where( Flex::where('description', 'LIKE', 'Starts with%'); );
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
有关详细信息,请参阅 贡献。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。