<?php

namespace App\Traits;

use App\Models\Location;

trait Locationable
{

    public function addLocation(array $location)
    {
        count($this->location()->get()) ? $this->location()->update(
            [
                'long' => $location['long'],
                'lat' => $location['lat'],
                'address' => $location['address'],
            ]
        ) : $this->location()->create(
            [
                'long' => $location['long'],
                'lat' => $location['lat'],
                'address' => $location['address'],
            ]
        );

    }

    public function location()
    {
        return $this->morphOne(Location::class, 'locationable');
    }
}
