When creating an eloquent model:
Model::create(['prop1' => 1, 'prop2' => 2]);
the returned model will only have prop1 and prop2 as properties, what can I do to load all the other properties that I did not insert into the database, because they are optional?
EDIT: Why do I need this? 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 extends Model { protected $table = 'tblCustomer'; protected $primaryKey = 'pkCustomerID'; public $timestamps = false; protected $attributes = [ 'baccount' => null, 'fldName' => null, 'fldNumRue' => null, ]; protected $appends = [ 'id', 'code', 'name', 'addressno' ]; public function __construct(array $attributes = []) {
The problem is that when Laravel converts everything to JSON, it will parse all my mutators:
public function getAddressnoAttribute() { return $this->attributes['fldNumRue']; }
and raise an error because $this->attributes['fldNumRue'] not defined ErrorException: Undefined index ... Therefore, I need to initialize all the attributes with their default values.
source share