Skip to content

Saving and Updating

Shashank Jain edited this page Oct 1, 2016 · 2 revisions

Saving and updating work out of the box. Consider this minimalist controller:

class UserController extends ApiController {
    protected $model = User::class;
}

Both, saving of new users and updating of existing ones will work with this. As usual:

  • to save a new user, send a POST request to the /user endpoint
  • to update a new user, send PUT request to the /user endpoint

Excluding Fields

By default, all the fields in the request are assumed to be model attributes. If certain fields being sent are to be excluded, you need to specify them in the excludes option in the config file.

Guarding Fields

There may be some fields, which you would not want to be updated from a request. For example, remember_token, created_at, etc. Such fields should be added to the $guarded array of the model.

Modifying Attributes

Of course, many times you would like to modify attributes received in a request before saving them. To do this, use eloquent model's mutators.