<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use App\Models\Transaksi;
class Armada extends Model
{
use HasFactory;
use SoftDeletes;
protected $guarded = [];
protected static function boot() {
parent::boot();
static::creating(function ($model) {
if ( ! $model->getKey()) {
$model->{$model->getKeyName()} = (string) Str::uuid();
}
});
}
protected $appends = [
'status'
];
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return 'string';
}
public function perusahaan()
{
return $this->belongsTo(Perusahaan::class);
}
public function detailTransaksi()
{
return $this->hasMany(DetailTransaksi::class);
}
public function getStatusAttribute(){
$status = '';
$onTrip = Transaksi::whereIn('status', ['muat', 'dijadwal', 'berangkat', 'datang'])->whereHas('detailTransaksi', function($dt){
$dt->where('armada_id', $this->id);
})->count();
if( $onTrip > 0 ){
$status = 'On A Trip';
}elseif($this->status_kerusakan == '1'){
$status = 'Under Repair';
}else{
$status = 'Ready';
}
return $status;
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]