creativeorange/laravel-injectable

将变量注入从数据库检索后的模型。

v3.0.0 2024-08-19 08:52 UTC

This package is auto-updated.

Last update: 2024-09-19 08:59:19 UTC


README

Total Downloads Latest Stable Version License

一个用于在从数据库检索后注入变量到模型的包。

安装

您可以通过composer安装此包。

composer require creativeorange/laravel-injectable

对于Laravel 8及以下版本,您应该需要版本1。

如果您正在使用Spaties Laravel Translatable,此包需要一个额外的插件,请阅读以下说明:Laravel Injectable translatable扩展

用法

在这个示例中,我们使用一个Question模型。此模型有两个属性:namedescription

变量是如何存储的

底层,我们使用Laravel的翻译系统和辅助方法__()。因此,我们需要将变量存储为:variable_name。在本README中,我们使用变量:company_name

设置模型

首先,请确保模型上的$casts属性已填写。

protected $casts = [
    'name'          => \Creativeorange\LaravelInjectable\Casts\InjectableCast::class,
    'description'   => \Creativeorange\LaravelInjectable\Casts\InjectableCast::class
];

之后,我们可以替换从数据库检索的属性中的变量。

按属性

$question = \App\Models\Question::first();

$question->name->inject('company_name', 'Acme Inc.');

// or to use multiple:
$question->name->inject(['company_name', 'Acme Inc.', 'another_var' => 'Another val']);

echo $question->name; // will output the name with the variables replaced
echo $question->description; // will output the original text, without any replacements; for example: This is the description for company :company_name

如上图所示,只有name属性将包含替换,而description仍显示数据库中的原始文本。

按模型

为此,模型需要使用App\InjectableTrait特质。之后,模型上将有一个可用的inject方法。

$question = \App\Models\Question::first();

$question->inject('company_name', 'Acme Inc.');
// or to use multiple:
$question->inject(['company_name', 'Acme Inc.', 'another_var' => 'Another val']);

echo $question->name; // will output the name with the variables replaced
echo $question->description; // will output the description with the variables replaced

在这个示例中,所有变量都将替换由App\Casts\InjectableCast转换的字段。

按请求周期

另一个用例是您想在请求级别替换所有变量。

// Use the alias
\LaravelInjectable::set('company_name', 'Acme Inc.');

// Or directly use the facade
\CreativeOrange\LaravelInjectable\Facades\LaravelInjectable::set('company_name', 'Acme Inc.');

// Or via the app method
app(\Creativeorange\LaravelInjectable\LaravelInjectable::class)->set('company_name', 'Acme Inc.');

// Or set multiple vars at once
\LaravelInjectable::set(['company_name', 'Acme Inc.', 'another_var' => 'Another val']);
\LaravelInjectable::set('company_name', 'Acme Inc.')->set('another_var', 'Another val');

$question = \App\Models\Question::first();
echo $question->name; // will output the name with the variables replaced
echo $question->description; // will output the description with the variables replaced

bonus:Blade指令:按请求周期

与上面相同,但然后在blade中

@inj('company_name', 'Acme Inc.')
//or to use multiple
@inj(['company_name', 'Acme Inc.', 'another_var' => 'Another val'])

@php($question = \App\Models\Question::first());
{{ $question->name }} {{-- will output the name with the variables replaced --}} 
{{ $question->description}} {{-- will output the description with the variables replaced --}}

测试

composer test

变更日志

请参阅CHANGELOG了解最近更改的详细信息。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件了解详细信息。