%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/forge/api-takeaseat.eco-n-tech.co.uk/app/Models/
Upload File :
Create Path :
Current File : //home/forge/api-takeaseat.eco-n-tech.co.uk/app/Models/UserAddress.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserAddress extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'address',
        'town_city',
        'slug',
        'postcode',
        'lat',
        'lng',
        'is_default',
        'address_type'
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'is_default' => 'boolean',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'formatted'
    ];

    /**
     * The different address types
     * 
     * @var array
     */
    const ADDRESS_TYPES = [
        1 => 'Personal',
        2 => 'Business',
        3 => 'Practice Location'
    ];

    /**
     * Get the user's full name.
     *s
     * @return string
     */
    public function getFormattedAttribute()
    {
        return "{$this->address}, {$this->town_city}, {$this->postcode}";
    }

    /**
     * Scope a query to only addresses within a distance
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeisWithinDistance($query, $coordinates, $radius = 30)
    {
        $haversine = "(6371 * acos(cos(radians(" . $coordinates['lat'] . ")) 
                    * cos(radians(`lat`)) 
                    * cos(radians(`lng`) 
                    - radians(" . $coordinates['lng'] . ")) 
                    + sin(radians(" . $coordinates['lat'] . ")) 
                    * sin(radians(`lat`))))";

        return $query->select('*')
            ->selectRaw("{$haversine} AS distance")
            ->whereRaw("{$haversine} < ?", [$radius]);
    }

    /**
     * Get the user that owns this address
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Zerion Mini Shell 1.0