如何在Laravel5.0中为Controller方法设置隐藏属性?

时间:2016-12-09 09:13:04

标签: laravel-5 laravel-eloquent

我想隐藏一些从数据库中选择的数据,但是从未在其模型中定义的Controller中的某些方法重新初始化。

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();
    }

1 个答案:

答案 0 :(得分:0)

你的要求不是很清楚。然而。我假设您有3个模型Client hasOne Contact和belongsTo Employer。

为了隐藏客户端模型的use_id属性,您可以在模型中定义隐藏属性

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');
    }
}  

然后可能在您的ClientsController中执行某些操作ddd

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