ldc-wenn-dann-mit-respekt/app/Models/Store.php

99 lines
2 KiB
PHP
Raw Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $type_id
* @property integer $region_id
* @property string $state
* @property string $street
* @property string $zipcode
* @property string $city
* @property float $latitude
* @property float $longitude
2023-03-29 10:04:16 +00:00
* @property integer $order
2023-03-15 13:51:01 +01:00
* @property string $created_at
* @property string $updated_at
*/
class Store extends Model
{
2023-03-16 09:47:16 +01:00
use HasFactory;
2023-03-15 13:51:01 +01:00
/**
2024-07-16 18:21:29 +02:00
* The attributes that are mass assignable.
*
* @var array<int, string>
2023-03-15 13:51:01 +01:00
*/
protected $fillable = [
'type_id',
'region_id',
'state',
'street',
'zipcode',
'city',
'latitude',
'longitude',
2023-03-29 10:04:16 +00:00
'order',
2023-03-15 13:51:01 +01:00
'created_at',
'updated_at'
];
2023-05-30 20:26:00 +02:00
/**
* The attributes that should be cast.
*
2024-01-29 16:02:03 +01:00
* @var array<string, string>
2023-05-30 20:26:00 +02:00
*/
protected $casts = [
'latitude' => 'float',
'longitude' => 'float'
];
2023-03-15 13:51:01 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
2023-03-29 10:04:16 +00:00
self::setOrder($model);
2023-03-15 13:51:01 +01:00
});
}
2023-03-29 10:04:16 +00:00
// * METHODS
/**
* Set order after the last element of the list.
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-29 10:04:16 +00:00
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function setOrder(self $model): void
2023-03-29 10:04:16 +00:00
{
$model->order = \intval(self::query()->max('order')) + 1;
}
2023-03-15 13:51:01 +01:00
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function storeType()
{
2023-05-05 10:07:01 +02:00
return $this->belongsTo(StoreType::class, 'type_id');
2023-03-15 13:51:01 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function storeRegion()
{
2023-05-05 10:07:01 +02:00
return $this->belongsTo(StoreRegion::class, 'region_id');
2023-03-15 13:51:01 +01:00
}
}