2017-07-17 31 views

回答

0

你可以通过字段名的数组all(),或者你可以在你的模型覆盖all()功能,并通过在诸如$柱阵列参数的字段名称为默认值:

/** 
* Get all of the models from the database. Overrided from Laravel 5.1.27 
* 
* @param array|mixed $columns // this is where you override instead of '*' 
* @return \Illuminate\Database\Eloquent\Collection|static[] 
*/ 
public static function all($columns = ['id','name','description']) 
{ 
    $columns = is_array($columns) ? $columns : func_get_args(); 

    $instance = new static; 

    return $instance->newQuery()->get($columns); 
} 

其他方式解决这个问题是在你的模型中创建一个示波器,并在这里使用它,如Prov::myFields()->get()

1

all方法返回所有行及其所有属性。如果你想隐藏json的一些字段,只需将它们添加到模型中受保护的$hidden数组中。

protected $hidden = ["created_at", "updated_at"]; 

但是,如果你想隐藏他们从php雄辩的模型,你可以添加方法来这个模型,从模型中取消给定的属性。然后通过对每一个项目或使用集合映射您的收藏和消防自定义方法循环:

$modified_items = Prov::all()->map(function($item){ 
    return $item->myUnsettingMethod(); 
}) 

当然,你仍然可以进行直接查询:

$provs = DB::table('provs')->select('some_column', 'another_column as ac')->get();