I am creating an API using laravel 10 and eloquent and I want to copy values from an object read from the http request to anoter object that is a laravel entity, I have an object with a lot of properties and I would like to copy automatically all the properties to another object so I can save the changes to the DB without copying values from request to model using $obj1->name = $request->name for each property in my object.
Let me explain:
I have this class:
class Usuarios extends Model
{
protected $table = 'usuarios';
protected $primaryKey = 'id';
protected $fillable = [... a lot of properties]
}
I have this method in my controller:
function save(Request $request){
$data = $request->all();
$user = new Usuario($data);
$userFromDB = Usuarios::find($data->id);
// is there a way to do this? I just want to copy properties
// without creating a new object, or is there another way to do this?
// using reflection or something like that? or does laravel have
// any other way to do this without copying each value??
copy_values($user,$userFromDB); //????????????
$userFromDB->save();
}
is this possible?
Thanks
Aucun commentaire:
Enregistrer un commentaire