2016-12-09 9 views
0

データベースから選択したいくつかのデータを非表示にしたいが、そのモデルで定義されていないコントローラの一部のメソッドから再初期化したい。Laravel5.0のコントローラメソッドに隠し属性を設定するにはどうすればよいですか?

function ddd(){ 
     return Client::select($this->_client)->with([ 
      'Contact'=>function($s){ 
       //$this->setHidden('use_id'); 
       //$s->setHidden('use_id'); 
       $s->select($this->_contact); 
      }, 
      'Employer'=>function($s){$s->select($this->_employers);}, 
     ])->get(); 
    } 

答えて

0

要件はあまり明確ではありません。しかしながら。私はあなたが3つのモデルのクライアントhasOneコンタクトとbelongsToの雇用者を持っていると仮定しています。あなたがにOKであるいくつかのアクションがddd

public function ddd() 
{ 
    return Client::with('contact')->with('employer')->get(); 
} 
+0

におそらくあなたのClientsControllerに

class Client extends Model { //will hide the 'use_id' from the model's array and json representation. protected $hidden = ['use_id']; //Relations /** * Get the Contact which the given Client has. * @return \Illuminate\Database\Eloquent\Relations\HasOne * will return a App\Contact model */ public function contact() { return $this->hasOne('App\Contact'); } /** * Get the Employee to which the given Client belongs. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo * will return a App\Employer model */ public function employer() { return $this->belongsTo('App\Employer'); } } 

次に、あなたのモデルに隠されたプロパティを定義することができ、クライアントモデルのuse_idプロパティを隠すために

しかし、私は関数またはマニュアル宣言の内側に宣言したいが、私はまだこの解決策を見つけることができない –

+1

私はあなたがいくつかの他のreqすべてのフィールドを返す必要があります。その場合は、SELECTステートメントを使用してクエリビルダとクエリでraw SQLクエリを使用できます。ここでは、制限された列のみを返すコントローラの制限付き列を指定できます。 – Donkarnash

関連する問題