0

我想隐藏从数据库中选择的一些数据,但从 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();
    }
4

1 回答 1

0

You requirement is not very clear. However. I am assuming that you have 3 models Client hasOne Contact and belongsTo Employer.

In order to hide the use_id property of the Client model you can define a hidden property in your model

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

Then in probably your ClientsController in some action ddd

public function ddd()
{
    return Client::with('contact')->with('employer')->get();
}
于 2016-12-11T13:27:25.697 回答