2017-07-17 78 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();