fleetCatalogue/app/Models/Vehicle.php

330 lines
7.8 KiB
PHP

<?php
namespace App\Models;
use Database\Factories\VehicleFactory;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Schema;
/**
* App\Models\Vehicle
*
* @property int $vehicle_id
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string $name
* @property int $vehicle_storage
* @property int $vehicle_fuel_volume
* @property int $vehicle_seats
* @property float|null $vehicle_price
* @property float|null $vehicle_top_speed
* @property int $vehicle_class_id
* @property int $fuel_type_id
* @property int $brand_id
* @property int $vendor_id
* @method static VehicleFactory factory($count = null, $state = [])
* @method static Builder|Vehicle filter(array $filters)
* @method static Builder|Vehicle newModelQuery()
* @method static Builder|Vehicle newQuery()
* @method static Builder|Vehicle query()
* @method static Builder|Vehicle whereBrandId($value)
* @method static Builder|Vehicle whereCreatedAt($value)
* @method static Builder|Vehicle whereFuelTypeId($value)
* @method static Builder|Vehicle whereName($value)
* @method static Builder|Vehicle whereUpdatedAt($value)
* @method static Builder|Vehicle whereVehicleClassId($value)
* @method static Builder|Vehicle whereVehicleFuelVolume($value)
* @method static Builder|Vehicle whereVehicleId($value)
* @method static Builder|Vehicle whereVehiclePrice($value)
* @method static Builder|Vehicle whereVehicleSeats($value)
* @method static Builder|Vehicle whereVehicleStorage($value)
* @method static Builder|Vehicle whereVehicleTopSpeed($value)
* @method static Builder|Vehicle whereVendorId($value)
* @property string $vehicle_name
* @method static Builder|Vehicle whereVehicleName($value)
* @mixin Eloquent
*/
class Vehicle extends Model
{
use HasFactory;
public function scopeFilter(
$query,
array $filters
): void {
$search = $filters['search'] ?? false;
if ($search) {
$query->where('vehicle_name', 'like', '%' . $search . '%');
}
unset($filters['search']);
$orderByOne = $filters['orderByOne'] ?? null;
$orderByTwo = $filters['orderByTwo'] ?? null;
if (!empty($orderByOne) || !empty($orderByTwo)) {
$orderBy = '';
if (!empty($orderByOne) && Schema::hasColumn('vehicles', $orderByOne)) {
$orderBy .= $orderByOne;
}
if (!empty($orderByTwo) && Schema::hasColumn('vehicles', $orderByTwo)) {
$orderBy .= empty($orderBy) ? $orderByTwo : ', ' . $orderByTwo;
}
$query->orderBy($orderBy);
}
foreach ($filters as $field => $filter) {
if (empty($filter)) {
continue;
}
switch ($field) {
case 'vehicle_class_id':
$query->join('vehicle_classes', 'vehicles.vehicle_class_id', '=', 'vehicle_classes.vehicle_class_id');
$query->where('vehicle_class_name', '=', $filter);
break;
case 'brand_id':
$query->join('brands', 'vehicles.brand_id', '=', 'brands.brand_id');
$query->where('brand_name', '=', $filter);
break;
case 'fuel_type_id':
$query->join('fuel_types', 'vehicles.fuel_type_id', '=', 'fuel_types.fuel_type_id');
$query->where('fuel_type_name', '=', $filter);
break;
case 'vendor_id':
$query->join('vendors', 'vehicles.vendor_id', '=', 'vendors.vendor_id');
$query->where('vendor_name', '=', $filter);
break;
default:
Schema::hasColumn('vehicles', $field);
$query->where($field, '=', $filter);
}
}
}
/**
* @return int
*/
public function getVehicleId(): int
{
return $this->vehicle_id;
}
/**
* @param int $vehicle_id
*
* @return Vehicle
*/
public function setVehicleId(int $vehicle_id): Vehicle
{
$this->vehicle_id = $vehicle_id;
return $this;
}
/**
* @return string
*/
public function getVehicleName(): string
{
return $this->vehicle_name;
}
/**
* @param string $vehicle_name
*
* @return Vehicle
*/
public function setVehicleName(string $vehicle_name): Vehicle
{
$this->vehicle_name = $vehicle_name;
return $this;
}
/**
* @return int
*/
public function getVehicleStorage(): int
{
return $this->vehicle_storage;
}
/**
* @param int $vehicle_storage
*
* @return Vehicle
*/
public function setVehicleStorage(int $vehicle_storage): Vehicle
{
$this->vehicle_storage = $vehicle_storage;
return $this;
}
/**
* @return int
*/
public function getVehicleFuelVolume(): int
{
return $this->vehicle_fuel_volume;
}
/**
* @param int $vehicle_fuel_volume
*
* @return Vehicle
*/
public function setVehicleFuelVolume(int $vehicle_fuel_volume): Vehicle
{
$this->vehicle_fuel_volume = $vehicle_fuel_volume;
return $this;
}
/**
* @return int
*/
public function getVehicleSeats(): int
{
return $this->vehicle_seats;
}
/**
* @param int $vehicle_seats
*
* @return Vehicle
*/
public function setVehicleSeats(int $vehicle_seats): Vehicle
{
$this->vehicle_seats = $vehicle_seats;
return $this;
}
/**
* @return float|null
*/
public function getVehiclePrice(): ?float
{
return $this->vehicle_price;
}
/**
* @param float|null $vehicle_price
*
* @return Vehicle
*/
public function setVehiclePrice(?float $vehicle_price): Vehicle
{
$this->vehicle_price = $vehicle_price;
return $this;
}
/**
* @return float|null
*/
public function getVehicleTopSpeed(): ?float
{
return $this->vehicle_top_speed;
}
/**
* @param float|null $vehicle_top_speed
*
* @return Vehicle
*/
public function setVehicleTopSpeed(?float $vehicle_top_speed): Vehicle
{
$this->vehicle_top_speed = $vehicle_top_speed;
return $this;
}
/**
* @return int
*/
public function getVehicleClassId(): int
{
return $this->vehicle_class_id;
}
/**
* @param int $vehicle_class_id
*
* @return Vehicle
*/
public function setVehicleClassId(int $vehicle_class_id): Vehicle
{
$this->vehicle_class_id = $vehicle_class_id;
return $this;
}
/**
* @return int
*/
public function getFuelTypeId(): int
{
return $this->fuel_type_id;
}
/**
* @param int $fuel_type_id
*
* @return Vehicle
*/
public function setFuelTypeId(int $fuel_type_id): Vehicle
{
$this->fuel_type_id = $fuel_type_id;
return $this;
}
/**
* @return int
*/
public function getBrandId(): int
{
return $this->brand_id;
}
/**
* @param int $brand_id
*
* @return Vehicle
*/
public function setBrandId(int $brand_id): Vehicle
{
$this->brand_id = $brand_id;
return $this;
}
/**
* @return int
*/
public function getVendorId(): int
{
return $this->vendor_id;
}
/**
* @param int $vendor_id
*
* @return Vehicle
*/
public function setVendorId(int $vendor_id): Vehicle
{
$this->vendor_id = $vendor_id;
return $this;
}
}