Can’t delete an object when using a different database connection

I have a table

Can't delete an object when using a different database connection

When I delete, I kept getting

Can't delete an object when using a different database connection

Note : I connected to a specific database driver babies with a table name babySettings

I don’t know why I can’t delete this :

public function destroy($id)
{

    $inputs  = Request::all();
    $baby    = DB::connection('babies')->table("babySettings")->find($id);
    $oldBaby = $baby;

    $deleteLock = [1,2,3];


    if(in_array((int)$id,$deleteLock)) {
        return Redirect::to('/baby') ->with('error', 'F* off');
    }

    if($baby){

        $logs   = DB::connection('babies')->table("babyLogs")->where('babyId',$id)->get();
        
        if(count($logs) > 0){
            foreach ($logs as $log) {
                $log->delete(); 
            }

        }

        $baby->delete();    

    } 

    return Redirect::to($_SERVER['HTTP_REFERER']) ->with('success', $oldBaby->babyName . ' + (' . count($logs) . ') removed!');

}

Did I do something wrong ?

If I dd out the $baby, I seem to get it

{#501 ▼
  +"id": 93
  +"status": 0
  +"uuid": "28753a43-2c30-4ce0-b4db-b177a8d6ae3c"
  +"name": "John Doe"
  +"email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19b959e94b19696df929e9c">[email protected]</a>"
  +"password": "$2y$10$oIq.SwurET3JBLuXgSJwLOUAZADQTjOTcnPKijV1EmUNBnsreoLvm"
  +"phone": ""
  +"address": ""
  +"babyName": "tyty"
  +"babyDob": "2021-04-18"
  +"verifyCode": "EnQoq8cheWGL4a37wyhtaK6fevJRZGLHoCyZ&t=1618743364"
  +"babyProfilePath": "https://i.imgur.com/DF8G7HS.png"
  +"babyBgPath": null
  +"adminCode": "x895Gq"
  +"readOnlyCode": "Z1EVdd"
  +"created_at": "2021-04-18 07:56:04"
  +"updated_at": "2021-04-18 07:56:04"
  +"feedingInterval": 2
  +"displayMode": 2
  +"imgUrAlbumId": null
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When you use DB, it returs a collection of sstdClass objects.

The delete function isn’t available for stdClass objects. You can use DB to delete your records:

$logs = DB::connection('babies')->table("babyLogs")->where('babyId',$id)->get();

foreach ($logs as $log) {
    DB::connection('babies')->table('babyLogs')->where('id', $log->id)->delete();
}

Or, you can delete all with one query:

DB::connection('babies')->table("babyLogs")->where('babyId',$id)->delete();

The baby is also stdClass, so, you have to delete it using query builder or eloquent:

DB::connection('babies')->table("babySettings")->where('id', $id)->delete();

Method 2

You’re not using the Eloquent ORM by fetching a record using the DB::table method. The record you are fetching is just a standard object, so it has no method available to delete.

You could use Eloquent to fetch the record in a more structured way by binding it to an model, or you can use the DB helpers (Query Builder) to delete the record the same way you are fetching it.

https://laravel.com/docs/8.x/queries#delete-statements


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x