How are My Laravel Model Objects Changing To Disable Updating?

I'm first selecting the record from the db like $obj = MyDbObject::select(...)->first() and assign it to a variable. I'm then trying to update it like

                $obj->update([
                    'status' => 0
                ]);

but doing this throws an error "Array to string conversion". If I pull the record again the update works fine.

$o = MyDbObject::find($obj->id);
                $o->update([
                    'status' => 0
                ]);

If I dd() $obj or $o they are both instances of the same class.

What changes when I "SELECT" the object?

EDIT: added stack trace.

ErrorException {#2682
  #message: "Array to string conversion"
  #code: 0
  #file: "./vendor/laravel/framework/src/Illuminate/Support/Str.php"
  #line: 353
  #severity: E_NOTICE
  trace: {
    ./vendor/laravel/framework/src/Illuminate/Support/Str.php:353 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/QueryException.php:56 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/QueryException.php:39 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Connection.php:665 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Connection.php:624 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Connection.php:490 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Connection.php:423 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2721 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:820 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:745 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:660 { …}
    ./vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:607 { …}
    ./app/.../MyDbObject.php:217 {
      App\...\MyDbObject->myfunc(...)^
      ›                     $obj->update([
      ›                         'status' => 0,
      arguments: {
        $attributes: array:1 [ …1]
      }
    }

EDIT 2: adding more code for @lagbox

$obj->update(['status' => 0, 'col2' => 'test']);
array_map(function ($obj) {
// do stuff with $obj; then run an update on it.
}, MyDbObject::select("t1.information as info", "my_db_objects.*")
            ->leftJoin('table_1 as t1', function ($join) {
                return $join->on('my_db_objects.table_1_id', '=', 't1.id');
            })
            ->where('my_db_objects.status', '1')
            ->get()
            ->map(function($mdo) {
                ...
            })
            ->all()
);

1 answer

  • answered 2021-02-23 05:38 Manish Rajora

    try this

    $o = MyDbObject::where('column_name',$obj->id);
                $o->update([
                    'status' => 0
                ]);
    

    change 'find' with 'where'