Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching Eloquent Errors

I perform various eloquent statements throughout my app. eg:

User::find($id)->update($data);

Or:

User::create($data);

Or:

User::paginate(10);

I presume the best way to tackle potential errors is to use try/catch? If so, what should I be catching for eloquent errors?

like image 813
panthro Avatar asked Feb 21 '26 00:02

panthro


1 Answers

There is findOrFail() method which will be helpful in some situations. But what kind of errors at runtime do you expect from paginate(), update(), create()?

During development Laravel will throw an exception when something is wrong with your model, like MassAssignmentException when some property you're trying to update is missing in $fillable.

like image 67
Alexey Mezenin Avatar answered Feb 24 '26 22:02

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!