提问者:小点点

连接到其他数据库驱动程序时无法删除对象-Laravel


我有一张桌子

当我删除的时候,我一直得到

注意:我连接到一个特定的数据库驱动程序babies,其表名为babysettings

我不知道为什么我不能删除这个:

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!');

}

我做错什么了吗?

如果我把$baby挖出来,我好像就会得到它

{#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
}

共1个答案

匿名用户

当您使用db时,它返回一个sstdclass对象的集合。

delete函数对stdClass对象不可用。可以使用DB删除记录:

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

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

或者,您可以通过一个查询删除所有内容:

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

baby也是stdclass,因此,您必须使用query builder或Elecoquent删除它:

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