I have Cars
model which belongs to two other models Region, Subregion
. Cars Model:
function region(){
return $this->belongsTo('App\Models\Region');
}
function subRegion(){
return $this->belongsTo('App\Models\Subregion');
}
Subregion model:
class Subregion extends Model{
public $timestamps = false;
protected $fillable = [
'name'
];
public function cars(){
return $this->hasMany('App\Models\Car');
}
}
Region:
class Region extends Model{
public $timestamps = false;
protected $fillable = [
'name'
];
public function cars(){
return $this->hasMany('App\Models\Car');
}
}
I have query which selects Cars with their region and subregion:
$queryCar= Car::with(array(
'region'=>function($query){
$query->select(['id', 'name']);
},
'subregion'=>function($query){
$query->select(['id', 'name']);
}
))->orderBy('id', 'DESC');
return $queryCar->get();
Table cars
has columns region_id
, subregion_id
.
When I try to loop the cars result:
foreach ($cars as $p){
var_dump($p->subregion);
var_dump($p->region);
break;
}
For the region, I see the result, which is not NULL, but there is no subregion. I am sure that there is, because, if I do:
var_dump($p);
I can see and subregion_id
and if I query database with that id, there is a subregion row.
It is strange, because everything is equal for these two - Subregion/Region.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire