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

55
April 22, 2021, at 05:40 AM

I have a table

When I delete, I kept getting

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": "jdoe@gg.com"
  +"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
}
Answer 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();
Answer 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

READ ALSO
Selecting second child div while hovering on first child div in makeStyles Material UI

Selecting second child div while hovering on first child div in makeStyles Material UI

I have a parent div and two child divs, I want to apply CSS to the second child div when I hover on the first child divThis is the structure of the render method

61
I want to split data from array()inside a html container and put it in a foreach loop with html [closed]

I want to split data from array()inside a html container and put it in a foreach loop with html [closed]

Want to improve this question? Add details and clarify the problem by editing this post

50
CSS animations not working with certain properties [duplicate]

CSS animations not working with certain properties [duplicate]

This might totally be a very stupid question, but I'm self-taught and I have a lot of questions

77