簡體   English   中英

雄辯事件的多態關系

[英]Polymorphic relation on eloquent event

我有2個班級, CoordinateHunt

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_typelocatable_id在數據庫中not null

我的問題是,如何使用本機雄辯事件處理保存的事件並獲取相關模型。

在您的關系中,您應該首先創建坐標,然后創建尋線。

$coordinate->hunt()->create(array());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM