Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

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:

    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.

like image 243
kitensei Avatar asked Aug 21 '15 12:08

kitensei


1 Answers

You can call fresh() method on your model. 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:

class MyModel extends Model {
  protected $attributes = [
    'key' => 'default value'
  ];
}
like image 197
jedrzej.kurylo Avatar answered Sep 29 '22 07:09

jedrzej.kurylo