Laravel 5.2 eloquent returns 软删除记录

Laravel 5.2 eloquent returns soft deleted records

我正在使用 Laravel 5.2

我有一个 model 如下:

<?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 从同一模型获取记录,它也会 returns 软删除记录。我正在使用下面的代码来获取记录:

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

eloquent 正在构建 query 作为:

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 方法(它已被注释掉),因此特征永远无法正确初始化。我相信这也意味着您一直在删除的数据实际上正在从数据库中删除。

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