使用构造函数时不会触发特征
Trait is not fired when I use the constructor
当我使用 class 的构造函数时,我的特征没有被触发。我的项目使用 laravel 7.0
trait LogUserDB
{
public static function bootLogUserDB()
{
self::creating();
self::updating(
function ($model) {
$user = Auth::user();
if ($user !== null && $user->id >= 0 && Schema::hasColumn($model->table, 'updated_by')) {
$model->updated_by = $user->id;
}
});
self::deleting();
}
}
abstract class AbstractBaseModel extends Model
{
use LogUserDB;
public function loadInfoSchema(){}
}
那是我的class,我不能使用构造函数
class EmailsModel extends AbstractBaseModel
{
protected $table = 'cad_emails';
protected $primaryKey = "id";
public function __construtor($request){
/**
* If I create the constructor my field is not updated
*/
}
}
在我的控制器中
Class XYZController extends BaseModel
{
$emailModel = new EmailsModel()
$emailEdit = $emailModel->find(1);
$emailEdit->email = 'teste@test.com';
$emailEdit->save() // $emailEdit->update()
dd('end');
}
当我 运行 这个没有构造函数的例子时,我的字段 "updated_by" 使用用户 ID 更新,当我使用构造函数时,字段不会更新
问题是什么?我不知道为什么 updated_by by field 没有用构造函数更新。
首先你应该遵循Model.php
中的构造函数签名,即:
public function __construct(array $attributes = [])
{
// ...
}
所以,你的 model
应该是这样的:
public function __construtor(array $attributes = [], $request = null)
{
parent::__construct($attributes); // This is required
$this->request = $request;
}
要调用父级的构造函数,您必须调用 parent::__construct($attributes)
方法,因为 traits
引导是在 Model.php
文件的 constructor
方法中完成的。
如果你想现在构建你的模型,那么你可以使用这样的东西:
// Assumed you've $request in the scope
$emailModel = new EmailsModel([], $request);
为了更清楚,检查 Model.php
,你会看到这样的东西:
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
在这种情况下,bootIfNotBooted
方法会启动特征,因此您需要手动调用父构造函数。
一个建议:你不需要在构造函数中传递$request
,如果需要,你可以从模型中访问Request
外观。也许想想你的模型的设计决策。此外,还有一些方法可用于创建、更新 e.t.c,您可以使用这些方法而无需手动构建模型。
当我使用 class 的构造函数时,我的特征没有被触发。我的项目使用 laravel 7.0
trait LogUserDB
{
public static function bootLogUserDB()
{
self::creating();
self::updating(
function ($model) {
$user = Auth::user();
if ($user !== null && $user->id >= 0 && Schema::hasColumn($model->table, 'updated_by')) {
$model->updated_by = $user->id;
}
});
self::deleting();
}
}
abstract class AbstractBaseModel extends Model
{
use LogUserDB;
public function loadInfoSchema(){}
}
那是我的class,我不能使用构造函数
class EmailsModel extends AbstractBaseModel
{
protected $table = 'cad_emails';
protected $primaryKey = "id";
public function __construtor($request){
/**
* If I create the constructor my field is not updated
*/
}
}
在我的控制器中
Class XYZController extends BaseModel
{
$emailModel = new EmailsModel()
$emailEdit = $emailModel->find(1);
$emailEdit->email = 'teste@test.com';
$emailEdit->save() // $emailEdit->update()
dd('end');
}
当我 运行 这个没有构造函数的例子时,我的字段 "updated_by" 使用用户 ID 更新,当我使用构造函数时,字段不会更新
问题是什么?我不知道为什么 updated_by by field 没有用构造函数更新。
首先你应该遵循Model.php
中的构造函数签名,即:
public function __construct(array $attributes = [])
{
// ...
}
所以,你的 model
应该是这样的:
public function __construtor(array $attributes = [], $request = null)
{
parent::__construct($attributes); // This is required
$this->request = $request;
}
要调用父级的构造函数,您必须调用 parent::__construct($attributes)
方法,因为 traits
引导是在 Model.php
文件的 constructor
方法中完成的。
如果你想现在构建你的模型,那么你可以使用这样的东西:
// Assumed you've $request in the scope
$emailModel = new EmailsModel([], $request);
为了更清楚,检查 Model.php
,你会看到这样的东西:
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
在这种情况下,bootIfNotBooted
方法会启动特征,因此您需要手动调用父构造函数。
一个建议:你不需要在构造函数中传递$request
,如果需要,你可以从模型中访问Request
外观。也许想想你的模型的设计决策。此外,还有一些方法可用于创建、更新 e.t.c,您可以使用这些方法而无需手动构建模型。