Add

Store a new model

Controllers with any of these Traits have method store() which a route can connect to for creating a model.

<?php
// ...

Route::post('/tasks', 'TaskController@store');

Validation

Method validationRules( ... ) is available for specifying the validation rules for the store() method.

<?php
// ...

public function validationRules(array $data, $id = null) {
  return [
    'title' => "required|unique:tasks,title"
  ];
}

📘

Tips

  • Parameter $id will always be null when creating a new model.
  • To use a different model apart from that provided by method model(), implement storeModel() to return the desired model.

Before store

To catch the model before it is updated, implement method beforeStore(&$data).

<?php
//...

public function beforeStore(array &$data) {
  // do something with/on $data
}

📘

Tip

  • If the method returns a non-null value, the store process is terminated and the returned value is sent to the client as the response.

Before response

After the model has been stored, it can be accessed with the beforeStoreResponse( ... ) method before it is sent to the client.

<?php
// ...

public function beforeStoreResponse(Illuminate\Database\Eloquent\Model &$data) {
  // do something with/on $data
}

📘

Tip

If the method returns a non-null value, the returned value is sent to the client as the response.

Exceptions

Exceptions are caught in the store() method and the store process is rolled back. To perform more operations when rolling back, implement method rollbackStore($data).

<?php
// ...

public function rollbackStore(array $data, Illuminate\Database\Eloquent\Model $model) {
  // do some further rolling back
}

Next