1

In one of my Laravel project, I have to update two table's data within a same scope of a function. At first query, I am trying to update data of a table using following query -

        DB::table('parameters')
        ->where($where)
        ->update($data);

In second and third query I am adding and subtracting two column's of another table -

DB::table('categories')
            ->where(['id' => $data['category_id']])
            ->increment('parameters');

DB::table('categories')
            ->where(['id' => $previous_category_id])
            ->decrement('parameters');

Everything is working fine. But, now I want to do all these operations within one query execution.

user3384985
  • 2,975
  • 6
  • 26
  • 42

1 Answers1

0

As far as I understand this there is no way to do this with one query.

What you are basically doing with the DB:table facade is runnig a MySQL UPDATE statement like: UPDATE <table_name> SET <field_name>=<value> WHERE condition

As you can see an UPDATE query is pointed to exactly one table, not more. So for updating multiple tables you need to run multiple UPDATE queries.

codedge
  • 4,754
  • 2
  • 22
  • 38
  • Okay, I thought about that. I saw a problem - http://stackoverflow.com/questions/8765490/mysql-update-two-tables-at-once and thinking is there any way to do that, without using query builder. – user3384985 May 02 '16 at 11:07