V
V
Viktor K2020-05-08 09:00:13
Laravel
Viktor K, 2020-05-08 09:00:13

How to change model field on update event?

There is a model update event assigned to updated.
I'm trying to update some model attributes on change but can't do it. What am I doing wrong?

class TagEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $tag;

    public function __construct(Tag $tag)
    {
        $this->tag = $tag;
        event(new SocketMessage($tag));
        $geocode = new Geocode($tag->internal_latitude, $tag->internal_longitude);
        $geocode->reverse();
        $tag->airport = $geocode->airport_name;
        $tag->city = $geocode->city;
        $tag->country = $geocode->country;
        $this->calculateDistance();
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor K, 2020-05-08
@vuldozer

To change, you need to respond to the updating event. You can't change the model after the update

N
ninja op op, 2020-05-08
@kur4chyt

Try adding to the end:
$tag->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question