Я запрашиваю модель 'Royalty' и жду загрузки отношения 'Владелец'.Модель возвращает результат, включая отношения.
class Royalty extends Model
{
protected $guarded = [];
protected $dates = ['created_at', 'updated_at', 'date', 'calculated_at'];
protected $casts = [
'properties' => 'object',
];
public function owner()
{
return $this->belongsTo(Owner::class);
}
}
class Owner extends Model
{
use SoftDeletes;
protected $guarded = [];
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected $casts = [
'properties' => 'object',
];
public function royalties()
{
return $this->hasMany(Royalty::class);
}
}
$royalty = Royalty::with('owner')->where('id', 1)->first();
Результат, как и ожидалось, и отношение 'владелец' загружено:
Royalty {#542 ▼
#guarded: []
#dates: array:4 [▶]
#casts: array:1 [▶]
#connection: "development"
#table: "royalties"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:21 [▶]
#original: array:21 [▶]
#changes: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"owner" => Owner {#562 ▼
#guarded: []
#dates: array:4 [▶]
#casts: array:1 [▶]
#connection: "development"
#table: "owners"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▶]
#original: array:8 [▶]
#changes: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#forceDeleting: false
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
Однако, когда я вызываю отношение с $ royalty-> owner, результат равен NULL.Что я тут не так делаю?
dd($royalty->owner); NULL