Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/src/Expansions/CompanyExpansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fleetbase\Build\Expansion;
use Fleetbase\FleetOps\Models\Driver;
use Illuminate\Database\Eloquent\Relations\HasMany;

class CompanyExpansion implements Expansion
{
Expand All @@ -18,11 +19,11 @@ public static function target()
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return HasMany
*/
public static function drivers()
{
return function () {
return function (): HasMany {
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->hasMany(Driver::class);
};
Expand Down
22 changes: 12 additions & 10 deletions server/src/Expansions/UserExpansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Fleetbase\Build\Expansion;
use Fleetbase\FleetOps\Models\Contact;
use Fleetbase\FleetOps\Models\Driver;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class UserExpansion implements Expansion
{
Expand All @@ -19,55 +21,55 @@ public static function target()
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @return HasOne
*/
public static function driver()
{
return function () {
return function (): HasOne {
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->hasOne(Driver::class)->without('user');
};
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @return HasOne
*/
public static function currentDriverSession()
{
return function () {
return function (): HasOne {
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->driver()->where('company_uuid', session('company'));
};
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return HasMany
*/
public static function driverProfiles()
{
return function () {
return function (): HasMany {
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->hasMany(Driver::class);
};
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @return HasOne
*/
public static function customer()
{
return function () {
return function (): HasOne {
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->hasOne(Contact::class, 'user_uuid', 'uuid')->where('type', 'customer')->without('user');
};
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @return HasOne
*/
public static function contact()
{
return function () {
return function (): HasOne {
/** @var \Illuminate\Database\Eloquent\Model $this */
return $this->hasOne(Contact::class, 'user_uuid', 'uuid')->without('user');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Fleetbase\FleetOps\Http\Resources\v1\Concerns;

use Fleetbase\Support\Http;
use Fleetbase\Support\RequestRelations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\MissingValue;

/**
Expand Down Expand Up @@ -30,6 +32,13 @@ trait ResolvesPublicRelationFields
* model actually defines are kept — a parent's expansion must never reach
* this model's loader, where it raises RelationNotFoundException.
*
* The result is handed to `loadMissing()`, and Eloquent loads a name by
* calling it as a method on a blank model, so a name that is merely a
* method (`with=saveInstance`) would run it. When core's RequestRelations
* is available an Eloquent model keeps only declared relations, each
* segment named as the model's method (`service_area` → `serviceArea`).
* Older core releases without it keep the root-method check below.
*
* @return array<int, string>
*/
protected function requestedRelations($request): array
Expand All @@ -46,6 +55,10 @@ protected function requestedRelations($request): array

$with = array_values(array_filter(array_map('strval', $with), 'strlen'));

if ($this->resource instanceof Model && class_exists(RequestRelations::class)) {
return RequestRelations::eagerLoads($this->resource, $with);
}

return array_values(array_filter($with, fn (string $relation): bool => $this->definesRelation($relation)));
}

Expand Down Expand Up @@ -77,7 +90,7 @@ private function definesRelation(string $relation): bool
* loaded on the public endpoints, so the key was absent unless `with` named
* it. Absent stays absent; it never becomes a string.
*
* @param array<int, string> $with camelCased relations the caller asked for
* @param array<int, string> $with the relation paths requestedRelations() kept, named as the model's methods
*/
protected function publicRelationObject(string $relation, array $with, \Closure $resource): mixed
{
Expand Down
5 changes: 1 addition & 4 deletions server/src/Models/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ public function company()
return $this->belongsTo(\Fleetbase\Models\Company::class);
}

/**
* @return BelongsTo
*/
public function vehicle()
public function vehicle(): BelongsTo
{
return $this->belongsTo(Vehicle::class)->select([
'uuid',
Expand Down
43 changes: 11 additions & 32 deletions server/src/Models/Fleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Fleetbase\Traits\Searchable;
use Fleetbase\Traits\SendsWebhooks;
use Fleetbase\Traits\TracksApiCredential;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Notifications\Notifiable;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
Expand Down Expand Up @@ -105,66 +108,42 @@ public function getSlugOptions(): SlugOptions
*/
protected $hidden = [];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function photo()
public function photo(): BelongsTo
{
return $this->belongsTo(\Fleetbase\Models\File::class, 'image_uuid', 'uuid');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function serviceArea()
public function serviceArea(): BelongsTo
{
return $this->belongsTo(ServiceArea::class)->select(['uuid', 'public_id', 'name', 'type', 'border']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function zone()
public function zone(): BelongsTo
{
return $this->belongsTo(Zone::class)->select(['uuid', 'public_id', 'name', 'border']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function vendor()
public function vendor(): BelongsTo
{
return $this->belongsTo(Vendor::class)->select(['uuid', 'public_id', 'name']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parentFleet()
public function parentFleet(): BelongsTo
{
return $this->belongsTo(Fleet::class)->without(['subFleets']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subFleets()
public function subFleets(): HasMany
{
return $this->hasMany(Fleet::class, 'parent_fleet_uuid', 'uuid')->without(['parentFleet']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function drivers()
public function drivers(): HasManyThrough
{
return $this->hasManyThrough(Driver::class, FleetDriver::class, 'fleet_uuid', 'uuid', 'uuid', 'driver_uuid');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function vehicles()
public function vehicles(): HasManyThrough
{
return $this->hasManyThrough(Vehicle::class, FleetVehicle::class, 'fleet_uuid', 'uuid', 'uuid', 'vehicle_uuid');
}
Expand Down
18 changes: 5 additions & 13 deletions server/src/Models/FuelReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Fleetbase\Traits\HasUuid;
use Fleetbase\Traits\Searchable;
use Fleetbase\Traits\TracksApiCredential;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class FuelReport extends Model
{
Expand Down Expand Up @@ -118,34 +119,25 @@ public function setAmountAttribute($value)
$this->attributes['amount'] = Utils::numbersOnly($value);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function driver()
public function driver(): BelongsTo
{
return $this->belongsTo(Driver::class)->without(['vehicle']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function vehicle()
public function vehicle(): BelongsTo
{
return $this->belongsTo(Vehicle::class)->without(['driver']);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function reportedBy()
{
return $this->belongsTo(User::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function reporter()
public function reporter(): BelongsTo
{
return $this->belongsTo(User::class, 'reported_by_uuid');
}
Expand Down
20 changes: 4 additions & 16 deletions server/src/Models/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ public function reportedBy()
return $this->belongsTo(User::class);
}

/**
* @return BelongsTo
*/
public function reporter()
public function reporter(): BelongsTo
{
return $this->belongsTo(User::class, 'reported_by_uuid');
}
Expand All @@ -168,26 +165,17 @@ public function assignedTo()
return $this->belongsTo(User::class);
}

/**
* @return BelongsTo
*/
public function assignee()
public function assignee(): BelongsTo
{
return $this->belongsTo(User::class, 'assigned_to_uuid');
}

/**
* @return BelongsTo
*/
public function vehicle()
public function vehicle(): BelongsTo
{
return $this->belongsTo(Vehicle::class);
}

/**
* @return BelongsTo
*/
public function driver()
public function driver(): BelongsTo
{
return $this->belongsTo(Driver::class);
}
Expand Down
15 changes: 8 additions & 7 deletions server/src/Models/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Fleetbase\Traits\HasPublicId;
use Fleetbase\Traits\HasUuid;
use Fleetbase\Traits\TracksApiCredential;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -140,7 +143,7 @@ public function getReturnNameAttribute()
/**
* Entities in the payload.
*/
public function entities()
public function entities(): HasMany
{
return $this->hasMany(Entity::class);
}
Expand Down Expand Up @@ -194,23 +197,23 @@ public function order()
/**
* The address the shipment will be delivered to.
*/
public function dropoff()
public function dropoff(): BelongsTo
{
return $this->belongsTo(Place::class, 'dropoff_uuid')->whereNull('deleted_at')->withoutGlobalScopes();
}

/**
* The address the shipment will be delivered from.
*/
public function pickup()
public function pickup(): BelongsTo
{
return $this->belongsTo(Place::class, 'pickup_uuid')->whereNull('deleted_at')->withoutGlobalScopes();
}

/**
* The address the shipment will be sent to upon failed delivery.
*/
public function return()
public function return(): BelongsTo
{
return $this->belongsTo(Place::class)->withoutGlobalScopes();
}
Expand All @@ -233,10 +236,8 @@ public function currentWaypointMarker()

/**
* Waypoints between start and end.
*
* @return \Illuminate\Database\Eloquent\Concerns\HasManyThrough
*/
public function waypoints()
public function waypoints(): HasManyThrough
{
return $this->hasManyThrough(Place::class, Waypoint::class, 'payload_uuid', 'uuid', 'uuid', 'place_uuid')->whereNull('waypoints.deleted_at')->withoutGlobalScopes();
}
Expand Down
Loading
Loading