Port StdClass data to Model Port StdClass data to Model laravel laravel

Port StdClass data to Model


Something like this will work for you. Just adjust it to your needs:

public function newFromStd(stdClass $std){    // backup fillable    $fillable = $this->getFillable();    // set id and other fields you want to be filled    $this->fillable(['id', ... ]);    // fill $this->attributes array    $this->fill((array) $std);    // fill $this->original array    $this->syncOriginal();    $this->exists = true;    // restore fillable    $this->fillable($fillable);    return $this;}

then you can do eg.:

$user = with(new User)->newFromStd( DB::table('users')->first() );// or make it static if you like:$user = User::newFromStd( DB::table('users')->first() );