jeudi 25 août 2016

Laravel custom collumn, nested relationships

I have next model: Case:

class Cases extends Model {
    const UPDATED_AT = 'edit_date';
    const CREATED_AT = 'crt_date';

    protected $primaryKey = 'no';
    protected $guarded = ['no'];
    protected $table = 'assist';

    public function providers() {
        return $this->hasMany('App\CaseProviders', 'case_no');
    }

    public function claims() {
        return $this->hasMany('App\Claim', 'reference_no');
    }

}

CaseProviders:

class CaseProviders extends Model {

    public function detail() {
        //return $this->hasOne('App\Providers', 'code', 'code')->select(['code', 'name']);
        return $this->hasOne('App\Providers', 'code', 'code');
    }

}

Providers:

class Providers extends Model {

    protected $table = 'user_cards';

    public function country() {
        return $this->hasOne('App\Country','id','country');
    }


}

Country:

class Country extends Model {

    protected $primaryKey = 'id';
    protected $table = 'user_countries';

    public function city() {
        return $this->hasMany('App\City', 'country', 'id');
    }

}

City:

class City extends Model
{
    protected $table = 'user_cities';
}

and this code in controller:

$case = \App\Cases::where('no', $id)->with('providers.detail.country.city')->first();

Code works fine, but i not need select all collumn from each model. I need only this:

name,code - from detail

name - from country

name - from city.

How i can do it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire