Laravel / Eloquent - 防止 get() 方法返回 "deleted_at" 列

Laravel / Eloquent - Prevent "deleted_at" Column From Being Returned by get() Method

我的模型看起来像:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Location extends Model{

    use SoftDeletes;

    // Name of our database table
    protected $table = 'locations';

    // Column for soft deletes
    protected $dates = ['deleted_at'];

    // Columns that are mass assignable
    protected $fillable = ['name', 'address', 'city'];

}

当我调用 Location::get() 时,默认返回 deleted_at 列。

如何防止在未明确指定的情况下返回此列?

您可以像这样使用 $hidden 属性:

protected $hidden = ['deleted_at'];

来自 documentation :

Sometimes you may wish to limit the attributes, such as passwords, that are included in your model's array or JSON representation. To do so, add a $hidden property to your model: