16

I want a code for update multiple rows of database, somthing like this:

UPDATE values SET data='{"options":["male","female"],"default":"male"}' where project_id=1 and id=1;
UPDATE values SET data='{"options":["male","female"],"default":"male"}' where project_id=1 and id=2;
UPDATE values SET data='{"options":["male","female"],"default":"male"}' where project_id=1 and id=3;

After some hours i could get result with something like this in laravel framework:

$values = Value::where('project_id', $id)->get();
$sql = "";
foreach($request->fields as $field) {
  if(!empty($field->default)) { //New default value is set
    foreach($values as $value) {
      $data = json_decode($value->data, true); /**data column as json object in mysql database **/
      $data["default"] = $field->default;
      $data = json_encode($data);
      $sql .= "update ".DB::connection()->getDatabaseName().".values set data='".$data."' where id="."$value->id;";
    }
  }
}
DB::unprepared($sql);

but this code is not a good practice! So my question is

Is there any ORM way to do this better?!

2 Answers 2

25

Here is an easy way to do it.

$values = Value::where('project_id', $id)->update(['data'=>$data]);

I found it from this link

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Assuming all records should have the same values, this is the correct answer. You could change the link to the manual instead of another site.
-6
    $values = Value::where('project_id', $id)->get();
    $sql = "";
    foreach($request->fields as $field) {
      if(!empty($field->default)) { //New default value is set
        foreach($values as $value) {      
          $tmp=$value->data;
          $tmp->default = $field->default;
          $value->data = $tmp;
          $value->save();
        }
      }
    }

And in Value model use Mutator and Accessor, like this

public function getDataAttribute($value)
{
    return json_decode($value);
}
public function setDataAttribute($value)
{
    $this->attributes['data'] = json_encode($value);
}

See documentation http://laravel.com/docs/4.2/eloquent#accessors-and-mutators

Work - https://yadi.sk/i/BnC3HYozehoy2

8 Comments

Thank you very much @vitalik_74 but there is an error: Indirect modification of overloaded property Value::$data has no effect
Sorry, is wrong $this->attributes['dmm_month']. I do copy\paste from my project :) . I change in answer to $this->attributes['data']
I run this code. And change. Please, see my answer and screenshort.
But still same error for me! Is there any way to save all changes once after foreach? I think this way is slow when database records are much
Ok. My code and your not equal. Use $tmp=$value->data; $tmp->default = $field->default; $value->data = $tmp; $value->save();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.