0

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?

Karim Pazoki
  • 951
  • 1
  • 13
  • 34

1 Answers1

0

If you have the amounts in an array or collection, walk through that and filter those elements where the amount is 0. Than perform a mass insert.

If possible, please try to avoid the usage of DB, instead use the Model’s static create method.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zoli
  • 1,081
  • 1
  • 8
  • 28