%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/shaban/duassis/api/app/Models/
Upload File :
Create Path :
Current File : //var/www/html/shaban/duassis/api/app/Models/Field.php

<?php

namespace App\Models;

use App\Exceptions\AppException;
use App\Traits\Addressable;
use App\Traits\Taggable;
use Illuminate\Support\Facades\DB;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
use Spatie\Translatable\HasTranslations;

/**
 * @property integer id
 * @property string name
 * @property string description
 * @property integer capacity
 * @property integer client_id
 * @property User client
 * @property mixed categories
 * @property mixed utilities
 */
class Field extends BaseModel implements HasMedia
{
    use HasTranslations, HasMediaTrait, Addressable, Taggable;

    const MEDIA_COLLECTION = 'fields';

    public $translatable = [
        'name',
        'description',
    ];

    protected $fillable = [
        'name',
        'description',
        'capacity',
    ];

    protected $guarded = [
        'user_id',
    ];

    protected $casts = [
        'id'          => 'integer',
        'name'        => 'string',
        'description' => 'string',
        'capacity'    => 'integer',
        'client_id'   => 'integer',
    ];

    /**
     * @param $data
     * @throws AppException
     */
    public static function createItem($data)
    {
        try {
            DB::beginTransaction();
            /** @var Field $field */
            $field = Field::query()
                          ->make([
                              'name'        => $data['name'],
                              'description' => $data['description'],
                              'capacity'    => $data['capacity'],
                          ]);
            /** @var User $client */
            $client = User::query()
                          ->find($data['user_id']);
            if (!$client->isClient()) {
                throw new AppException('User should be a client');
            }
            if (!$client->address) {
                throw new AppException("Client doesn't have an address");
            }
            if (!$client->profile) {
                throw new AppException("Client doesn't have a profile");
            }
            $field->client()
                  ->associate($client);
            $field->save();
            $field->categories()
                  ->sync($data['categoriesId']);
            $field->utilities()
                  ->sync($data['utilitiesId']);
            $field->save();
            DB::commit();
            try {
                if (array_key_exists('image', $data) && $image = $data['image']) {
                    $field->uploadPicture($image);
                }
            } catch (\Exception $e) {
                //NOTHING
            }
            return $field;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function client()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function categories()
    {
        return $this->belongsToMany(Category::class, 'field_category', 'field_id', 'category_id');
    }
    public function utilities()
    {
        return $this->belongsToMany(Category::class, 'field_utility', 'field_id', 'utility_id');
    }

    /**
     * @param $base64data
     * @return $this
     * @throws FileCannotBeAdded
     */
    public function uploadPicture($base64data)
    {
        $name = md5(time());
        try {
            $this->addMediaFromBase64($base64data)
                 ->usingName($name)
                 ->usingFileName("$name.png")
                 ->toMediaCollection(self::MEDIA_COLLECTION, self::MEDIA_COLLECTION);
            return $this;
        } catch (FileCannotBeAdded $e) {
            throw $e;
        }
    }

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'model', 'taggables');
    }

    public function getPreview()
    {
        /** @var Media $media */
        if ($media = $this->getMedia(self::MEDIA_COLLECTION)
                          ->first()) {
            return asset($media->getUrl());
        }
        return asset('images/stadium.jpg');
    }

    public function scopeIsWithinMaxDistance($query, $coordinates, $radius = 5)
    {
        return $query->whereHas('address', function ($q) use ($coordinates, $radius) {
            $q->IsWithinMaxDistance($coordinates, $radius);
        })
                     ->orWhere(function ($q) use ($coordinates, $radius) {
                         $q->whereDoesntHave('address');
                         $q->whereHas('client', function ($u) use ($coordinates, $radius) {
                             $u->whereHas('address', function ($a) use ($coordinates, $radius) {
                                 $a->IsWithinMaxDistance($coordinates, $radius);
                             });
                         });
                     });
    }

}

Zerion Mini Shell 1.0