2

this might be a different question from the questions that has been asked here already about inserting arrays into the database, but mine is quite different.

Here's the code:

public function something(Request $request)
{
  $id = $request ->id;
  $name = $request->name;
  $money = $request ->money;

  $data = array(
    'name' => $name, 
    'money' => $money,
  );

  $z = User::where('id',$id)
    ->select('name','money')
    ->first();

  $data2 = array(
    'name' => $z->name,
    'money' => $z->money,
  );

  $diff = array_diff($data,$data2);
  $diff_name = array_keys($diff);
  $diff_values = array_values($diff);
  for ($i = 0; $i < count($diff_name); $i++) {
    $z->$diff_name[$i] = $diff_values[$i];
    $z->save();
  }
  return 'Success';
}

Model:

class User extends Model
{
    protected $table = 'users';
    protected $fillable = ['name','money'];
    public $timestamps = false;
}


$data:
array(2) { ["name"]=> string(9) "Somewhere" ["money"]=> string(7) "5123.00"}

 $data2:
array(2) { ["name"]=> string(8) "Anywhere" ["money"]=> string(7) "5000.00"}

So I'm using array_diff to determine if the values that has been entered by the user is equal from the original value from the database, if theres a difference in value then get the column name($diff_name) and then insert the new value($diff_value). I did it this way since I will be needing the values of $diff_name for history log purposes. However I'm getting this error Array to string conversion Thanks in advance.

5
  • Please include relevant code of your User model class, it may help us assist you better. There might be an easy way to ensure the value gets cast to a JSON string for insertion and parsed as array when accessing it. Check here. Commented Feb 17, 2020 at 17:39
  • @segFault added, hope it will help Commented Feb 17, 2020 at 17:44
  • What does your $data array look like? Can you var_dump() it and add to your question as well? I am thinking that those values are actually arrays and not strings. Commented Feb 17, 2020 at 17:52
  • @segFault edited. Commented Feb 17, 2020 at 17:57
  • hmm, not what I was expecting. So where is the error occurring exactly? Do you have the line where it complains? it might be nested in a stack trace, that might help too. Commented Feb 17, 2020 at 20:38

1 Answer 1

1

There is are several built in Eloquent methods that you could implement.


    public function something(Request $request)
    {
      $z = User::where('id',$request->id)
        ->select('name','money')
        ->first(); // You could also use '->firstOrFail()' if you want to stop execution if a model was not retrieved. 

      $z->fill([
        'name' => $request->name,
        'money' => $request->money,
      ]);


      $diff = array_keys($z->getDirty());

      $z->save()'

      return 'Success';
    }

$model->firstOrFail()

Can be used to stop execution to prevent an error from trying to manipulate a model which was not retrieved from the database.


$model->fill()

This can be used to pass an array of values which you would like to update.


$request->only(['name','money])

Instead of passing an array to $model->fill() you could have also used this.


$model->getDirty()

After changing a models attributes (and before saving it) you can use this to get an associative array of the attributes and corresponding values which have changed.

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

Comments

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.