Laravel / Eloquent - 防止get_()方法返回“deleted_at”列

时间:2017-11-30 05:11:43

标签: laravel laravel-5 eloquent laravel-5.2 laravel-eloquent

我的模型看起来像:

<?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列。

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

1 个答案:

答案 0 :(得分:1)

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

protected $hidden = ['deleted_at'];

来自documentation

  

有时您可能希望限制模型数组或JSON表示中包含的属性,例如密码。为此,请向模型添加$ hidden属性: