简体   繁体   English

Laravel 5创建后的雄辩负载模型属性

[英]Laravel 5 eloquent load model properties after create

When creating an eloquent model: 在创建雄辩的模型时:

Model::create(['prop1' => 1, 'prop2' => 2]);

the returned model will only have prop1 & prop2 as properties, what can I do to eager load all others properties that I haven't inserted in database because they are optional ? 返回的模型只有prop1prop2作为属性,我可以做什么来急切加载我没有插入数据库的所有其他属性,因为它们是可选的?

EDIT: Why do I need this ? 编辑:为什么我需要这个? to rename my database fields: 重命名我的数据库字段:

database 数据库

CREATE TABLE `tblCustomer` (
    `pkCustomerID` INT(11) NOT NULL AUTO_INCREMENT,
    `baccount` VARCHAR(400) NULL DEFAULT NULL,
    `fldName` VARCHAR(400) NULL DEFAULT NULL,
    `fldNumRue` VARCHAR(10) NULL DEFAULT NULL,
    ....
    PRIMARY KEY (`pkCustomerID`)
);

customer model 客户模型

<?php namespace App\Models;

/**
 * Class Customer
 * @package App\Models
 * @property int code
 * @property string name
 * @property string addressno
 */
class Customer extends Model
{
    protected $table = 'tblCustomer';
    protected $primaryKey = 'pkCustomerID';
    public $timestamps = false;

    /**
     * The model's attributes.
     * This is needed as all `visible fields` are mutators, so on insert
     * if a field is omitted, the mutator won't find it and raise an error.
     * @var array
     */
    protected $attributes = [
        'baccount'           => null,
        'fldName'            => null,
        'fldNumRue'          => null,
    ];

    /**
     * The accessors to append to the model's array form.
     * @var array
     */
    protected $appends = [
        'id',
        'code',
        'name',
        'addressno'
    ];

    public function __construct(array $attributes = [])
    {
        // show ONLY mutators
        $this->setVisible($this->appends);

        parent::__construct($attributes);
    }

    public function setAddressnoAttribute($value)
    {
        $this->attributes['fldNumRue'] = $value;
        return $this;
    }

    public function getAddressnoAttribute()
    {
        return $this->attributes['fldNumRue'];
    }
}

The problem is that when Laravel converts everything to JSON, he will parse all my mutators: 问题是,当Laravel将所有内容转换为JSON时,他将解析我的所有mutator:

    public function getAddressnoAttribute()
    {
        return $this->attributes['fldNumRue'];
    }

and raise an error as $this->attributes['fldNumRue'] is not defined ErrorException: Undefined index... So I need a way to initialize all attributes with their default values. 并且因为没有定义$this->attributes['fldNumRue']而引发错误ErrorException:未定义的索引...所以我需要一种方法来使用它们的默认值初始化所有属性。

You can call fresh() method on your model. 您可以在模型上调用fresh()方法。 It will reload the model from the database and return it. 它将从数据库重新加载模型并返回它。 Keep in mind that it returns a reloaded object - it doesn't update existing one. 请记住,它返回一个重新加载的对象 - 它不会更新现有的对象。 You can also pass an array of relations that should be reloaded: 您还可以传递应重新加载的关系数组:

$model = $model->fresh($relations);

You could consider removing default values from the database and in your model. 您可以考虑从数据库和模型中删除默认值。 This way you won't need to reload model to get the defaults. 这样您就不需要重新加载模型来获取默认值。

You can do it by overriding $attributes property in your model and seting defaults there: 你可以通过覆盖模型中的$ attributes属性并在那里设置默认值来实现:

class MyModel extends Model {
  protected $attributes = [
    'key' => 'default value'
  ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM