dimanche 7 avril 2019

Laravel: Check condition on a column before save it in database

I am using a script that has a insert query like this:

DB::table('documents')->insert('amount' => $amount, <some other columns>);

this query used over and over (more than 200) in some controllers.

Now I want to add a condition that if the value that assigned to 'amount' is 0 the insertion query be ignored.

A simple (but BAD) way is adding condition to all where insertion called. A better way is I define a function (in a library or something like that) and take query in it and change all controllers that currently insertion query is in there. But as I said before it is time-consuming.

Is there a better way (like accessor/mutator or something like to check a condition before executing this query?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire