gpt4 book ai didi

Laravel 5.2 Eloquent 地返回软删除记录

转载 作者:行者123 更新时间:2023-12-02 00:55:34 25 4
gpt4 key购买 nike

我正在使用 Laravel 5.2

我有一个模型如下:

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;

class ZoomMeeting extends BaseModel {

public $timestamps=true;

protected $table = 'zoom_meetings';

use SoftDeletes;

protected $dates = ['deleted_at'];

protected $fillable = ['user_id', 'uuid', 'meeting_id', 'host_id', 'topic', 'status', 'type', 'start_url', 'join_url', 'created_at'];

public function users() {
return $this->belongsTo('App\Models\User');
}
}

基础模型如下:

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Auth;
use Carbon\Carbon;

class BaseModel extends Model {

public $timestamps = false;

protected static function boot()
{
//parent::boot();

static::creating(function($model) {
if(empty($model->created_at))
{
$model->created_at = date('Y-m-d H:i:s');
}
return true;
});

static::updating(function($model) {
$model->updated_at = date('Y-m-d H:i:s');
return true;
});
}
}

我在 ZoomMeeting 模型中使用 softdeletetrait,软删除工作正常。

但是,如果我使用 Eloquent 从同一个模型中获取记录,它也会返回软删除的记录。我正在使用下面的代码来获取记录:

$record = ZoomMeeting::where("user_id", $user_id)->where("meeting_id", $meeting_id)->orderBy("id", "DESC")->first();

eloquent查询 构建为:

select * from `zoom_meetings` where `user_id` = 3 and `meeting_id` = 707070707 order by `id` desc limit 1

看,where 语句中没有设置deleted at is null。它不会阻止删除的记录。

我不确定我哪里出错了?

最佳答案

看起来你正在重写 boot 方法,但你实际上并没有调用父 boot 方法(它被注释掉了),所以特征是 never正确初始化。我相信这也意味着您一直在删除的数据实际上是从数据库中删除的。

是否有理由需要覆盖引导方法?您添加的内容已由框架处理,因此似乎没有必要。

关于Laravel 5.2 Eloquent 地返回软删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35626464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com