You can use Eloquent events https://laravel.com/docs/5.5/eloquent#events
class Travel extends Model
{
...
public static function boot() {
parent::boot();
static::creating( function( $model ) {
$model->formatMyPoint();
} );
static::updating( function( $model ) {
$model->formatMyPoint();
} );
}
protected function formatMyPoint() {
$this->my_point = DB::raw('POINT(' . $this->x . ' ' . $this->y . ')');
}
}
pangodream liked this reply
Thanks for your reply!
I've used the saving event, that is fired on create and update and it works fine, but I need to avoid Eloquent trying to save this->x and this->y to database. I think I can do it with unset(this->x/y) but I will prefer a cleaner way.
Regards.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community