[英]Polymorphic relation on eloquent event
我有2個班級, Coordinate
和Hunt
class Coordinate extends Model {
public function locatable(){
return $this->morphTo();
}
}
和
class Hunt extends Model {
public function coordinate() {
return $this->morphOne('App\Coordinate', 'locatable');
}
}
然后我使用創建一個新的模型實例
$cord = new Coordinate;
$hunt = new Hunt;
$hunt->save();
$hunt->coordinate->save($cord);
現在我聽保存的Hunt
事件
class HuntSaved {
use Dispatchable, InteractsWithSockets, SerializesModels;
public $hunt;
public function __construct(Hunt $hunt) {
dd($hunt->coordinate);
$this->hunt = $hunt;
}
}
奇怪的是,這里的dd
返回null
。
我也不能先保存Coordinate
因為它的locatable_type
和locatable_id
在數據庫中not null
。
我的問題是,如何使用本機雄辯事件處理保存的事件並獲取相關模型。
在您的關系中,您應該首先創建坐標,然后創建尋線。
$coordinate->hunt()->create(array());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.