我尝试使用其他一些字段扩展 extintig \xcb\x99PHP` Laravel 模型,但我没有找到正确的解决方案。我使用 PHP 7.1 和 Laravel 6.2
\n\n这是我的代码,解释了我想要做什么。
\n\n原型号:
\n\n<?php\nnamespace App;\n\nuse App\\Scopes\\VersionControlScope;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Product extends Model\n{\n protected $fillable = [\n \'product_id\',\n \'name\',\n \'unit\',\n // ...\n }\n\n // ... relations, custom complex functions are here\n}\n\nRun Code Online (Sandbox Code Playgroud)\n\n正如我想象的那样如何扩展原始模型:
\n\n<?php\nnamespace App;\n\nclass ProductBackup extends Product\n{\n protected $fillable = array_merge(\n parent::$fillable,\n [\n \'date_of_backup\',\n ]\n );\n\n // ...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但现在我收到Constant expression contains invalid operations错误消息。
$fillable我可以在子类中扩展显示原始模型的数组吗?
我正在尝试在我的项目中使用 Laravel 预加载,并且我已经阅读了有关它的文档。文档中的每个示例都是关于使用预加载获取模型的所有实例。但这只是获取所有实例而不仅仅是单个模型吗?考虑一下:
public function single(Coin $coin)
{
$coin = $coin->with('exchanges')->get();
return view('public.coin',compact('coin'));
}
Run Code Online (Sandbox Code Playgroud)
它是一种控制器方法,它使用路由绑定加载单个硬币并注入实例,我正在尝试急切加载该硬币的关系。但是当我在刀片视图文件中访问 $coin 时,我会得到所有硬币的列表。所以我不能急切地加载注入的模型实例?!
我有一些 Laravel 模型使用下一个 Trait 来调用模型的 boot 方法:
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait Uuids
{
/**
* Boot function from Laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = Str::uuid()->toString();
if ($model->consecutive) {
$model->consecutive = $model->max("consecutive") + 1;
}
}
});
}
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Get the auto-incrementing key type.
* …Run Code Online (Sandbox Code Playgroud) 在每个 Laravel 模型中,我想像下面这样声明可填充。
protected $fillable = [
'object_name','attributes','item','cost','status','category',
'object_id','status','customer_id','provider_id'
];
Run Code Online (Sandbox Code Playgroud)
之后,我可以在数据库中插入值。有什么方法可以使每个新创建的表的所有列都可填充,还是我每次都需要更改它?