vluzrmos / eloquent-prefixes
允许与前缀属性名称一起工作
v0.0.1
2016-02-06 18:34 UTC
Requires
- illuminate/database: ~5.0
This package is auto-updated.
Last update: 2024-09-13 05:38:01 UTC
README
该包允许您在eloquent模型上添加属性前缀。
安装
composer require vluzrmos/eloquent-prefixes
用法
您可以使用模型
use Vluzrmos\Database\Eloquent\ModelWithPrefixedAttributes as Model; class MyModel extends Model { /** * A string that should be used to prefix attributes. */ protected $attributesPrefix = "my_"; /** * Array of attributes to prefix */ protected $attributesToPrefix = [ 'name', 'email' ]; protected $fillable = [ 'name', 'email', // 'my_name', //only if you need it // 'my_email' //only if you need it ]; }
然后您可以使用
$model = MyModel::first();
$model->name; //same of $model->my_name;
$model->name = "Vluzrmos"; //same of $model->my_name = "Vluzrmos";
MyModel::create([
'name' => 'Vagner do Carmo',
'email' => 'my_email@gmail.com'
]);
// its the same of
MyModel::create([
'my_name' => 'Vagner do Carmo',
'my_email' => 'my_email@gmail.com'
]);
//Note:: that should be on your fillable array.
您也可以使用Trait,它的工作方式相同
ues Illuminate\Database\Eloquent\Model; use Vluzrmos\Database\Eloquent\PrefixesAttributes; class MyModel extends Model { use PrefixesAttributes; /** * A string that should be used to prefix attributes. */ protected $attributesPrefix = "my_"; /** * Array of attributes to prefix */ protected $attributesToPrefix = [ 'name', 'email' ]; protected $fillable = [ 'name', 'email', // 'my_name', //only if you need it // 'my_email' //only if you need it ]; }
待办事项
- 与查询构建器一起工作
- 与模型属性一起工作(自动访问器和修改器)