tinkering with the polymorphic eager load

This commit is contained in:
Godfrey M 2025-01-07 09:08:36 -08:00
parent 0ec25d55a0
commit e12c7473f8
4 changed files with 34 additions and 5 deletions

View file

@ -1117,7 +1117,8 @@ class ReportsController extends Controller
if ($showDeleted) {
$query->withTrashed()->with(['assignedTo']);
} else {
}
else {
$query->with(['assignedTo' => function ($query) {
$query->withTrashed();
}]);
@ -1129,10 +1130,10 @@ class ReportsController extends Controller
$filtered = $chunk->filter(function ($acceptance) {
$acceptance_checkoutable_flag = false;
if($acceptance->checkoutable) {
$acceptance_checkoutable_flag = $acceptance->assignedTo;
if ($acceptance->checkoutable){
$acceptance_checkoutable_flag = $acceptance->checkoutable->assignedTo();
}
// Return true if criteria match
// Return true if user
return $acceptance->checkoutable_type === 'App\Models\Asset' && $acceptance_checkoutable_flag;
})->map(function ($acceptance) {

View file

@ -0,0 +1,24 @@
<?php
namespace App\Http\Traits;
trait CheckoutableTrait
{
public const LOCATION = 'location';
public const ASSET = 'asset';
public const USER = 'user';
public function checkedOutToUser(): bool
{
return $this->assignedType() === self::USER;
}
public function checkedOutToLocation(): bool
{
return $this->assignedType() === self::LOCATION;
}
public function checkedOutToAsset(): bool
{
return $this->assignedType() === self::ASSET;
}
}

View file

@ -5,6 +5,7 @@ namespace App\Models;
use App\Events\CheckoutableCheckedOut;
use App\Exceptions\CheckoutNotAllowed;
use App\Helpers\Helper;
use App\Http\Traits\CheckoutableTrait;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\Acceptable;
use App\Models\Traits\Searchable;
@ -33,6 +34,7 @@ class Asset extends Depreciable
protected $with = ['model', 'adminuser'];
use CompanyableTrait;
use CheckoutableTrait;
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
public const LOCATION = 'location';

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\Http\Traits\CheckoutableTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -11,6 +12,7 @@ use Illuminate\Notifications\Notifiable;
class CheckoutAcceptance extends Model
{
use HasFactory, SoftDeletes, Notifiable;
use CheckoutableTrait;
protected $casts = [
'accepted_at' => 'datetime',
@ -35,7 +37,7 @@ class CheckoutAcceptance extends Model
/**
* The resource that was is out
*
* @return Illuminate\Database\Eloquent\Relations\MorphTo
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function checkoutable()
{