mardi 28 mai 2019

Best practice on indicating a model error to the controller in Laravel

Lets say I have my business logic in a Model, for example in Student.php (that extends Model).

In my controller, I get an instance of that model: $myStudent = Student::find(1);

Then I want to perform some action on it, say: $myStudent->graduate();

But my business logic says that because of some condition, this action can't be performed. How do I pass this information to the controller? Via a return value? Via an exception? Some other way?

  1. Return value: If I return a value (say, 0:success, 1:Error because average is below 70, 2:Error because average is below 50), part of the business logic would leak into the controller, which should be avoided.

  2. Exception: If the request is successful the normal execution of the code continues, but if not I throw an exception along with some text that is displayed to the user.

Is an exception the way to go? Any other way to do this? Thanks!



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2VUpGzd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire