Support the ongoing development of Laravel.io →
Eloquent Database
Last updated 1 year ago.
0

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

1

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.

Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Abel abelcs Joined 11 Oct 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.