I have 2 classes, Coordinate
and Hunt
class Coordinate extends Model {
public function locatable(){
return $this->morphTo();
}
}
and
class Hunt extends Model {
public function coordinate() {
return $this->morphOne('App\Coordinate', 'locatable');
}
}
Then I create a new instance of the models using
$cord = new Coordinate;
$hunt = new Hunt;
$hunt->save();
$hunt->coordinate->save($cord);
Now I listen to the saved event of the Hunt
class HuntSaved {
use Dispatchable, InteractsWithSockets, SerializesModels;
public $hunt;
public function __construct(Hunt $hunt) {
dd($hunt->coordinate);
$this->hunt = $hunt;
}
}
The strange thing is that the dd
here returns null
.
I cannot save the Coordinate
first due too its locatable_type
and locatable_id
which are not null
in the database.
My question is, how can I handle the saved event using native eloquent events and get the related model.
在您的关系中,您应该首先创建坐标,然后创建寻线。
$coordinate->hunt()->create(array());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.