tinkering with the polymorphic eager load
This commit is contained in:
parent
0ec25d55a0
commit
e12c7473f8
4 changed files with 34 additions and 5 deletions
|
@ -1117,7 +1117,8 @@ class ReportsController extends Controller
|
||||||
|
|
||||||
if ($showDeleted) {
|
if ($showDeleted) {
|
||||||
$query->withTrashed()->with(['assignedTo']);
|
$query->withTrashed()->with(['assignedTo']);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$query->with(['assignedTo' => function ($query) {
|
$query->with(['assignedTo' => function ($query) {
|
||||||
$query->withTrashed();
|
$query->withTrashed();
|
||||||
}]);
|
}]);
|
||||||
|
@ -1129,10 +1130,10 @@ class ReportsController extends Controller
|
||||||
$filtered = $chunk->filter(function ($acceptance) {
|
$filtered = $chunk->filter(function ($acceptance) {
|
||||||
$acceptance_checkoutable_flag = false;
|
$acceptance_checkoutable_flag = false;
|
||||||
|
|
||||||
if($acceptance->checkoutable) {
|
if ($acceptance->checkoutable){
|
||||||
$acceptance_checkoutable_flag = $acceptance->assignedTo;
|
$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;
|
return $acceptance->checkoutable_type === 'App\Models\Asset' && $acceptance_checkoutable_flag;
|
||||||
})->map(function ($acceptance) {
|
})->map(function ($acceptance) {
|
||||||
|
|
||||||
|
|
24
app/Http/Traits/CheckoutableTrait.php
Normal file
24
app/Http/Traits/CheckoutableTrait.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ namespace App\Models;
|
||||||
use App\Events\CheckoutableCheckedOut;
|
use App\Events\CheckoutableCheckedOut;
|
||||||
use App\Exceptions\CheckoutNotAllowed;
|
use App\Exceptions\CheckoutNotAllowed;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
|
use App\Http\Traits\CheckoutableTrait;
|
||||||
use App\Http\Traits\UniqueUndeletedTrait;
|
use App\Http\Traits\UniqueUndeletedTrait;
|
||||||
use App\Models\Traits\Acceptable;
|
use App\Models\Traits\Acceptable;
|
||||||
use App\Models\Traits\Searchable;
|
use App\Models\Traits\Searchable;
|
||||||
|
@ -33,6 +34,7 @@ class Asset extends Depreciable
|
||||||
protected $with = ['model', 'adminuser'];
|
protected $with = ['model', 'adminuser'];
|
||||||
|
|
||||||
use CompanyableTrait;
|
use CompanyableTrait;
|
||||||
|
use CheckoutableTrait;
|
||||||
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
|
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
|
||||||
|
|
||||||
public const LOCATION = 'location';
|
public const LOCATION = 'location';
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Http\Traits\CheckoutableTrait;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
@ -11,6 +12,7 @@ use Illuminate\Notifications\Notifiable;
|
||||||
class CheckoutAcceptance extends Model
|
class CheckoutAcceptance extends Model
|
||||||
{
|
{
|
||||||
use HasFactory, SoftDeletes, Notifiable;
|
use HasFactory, SoftDeletes, Notifiable;
|
||||||
|
use CheckoutableTrait;
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'accepted_at' => 'datetime',
|
'accepted_at' => 'datetime',
|
||||||
|
@ -35,7 +37,7 @@ class CheckoutAcceptance extends Model
|
||||||
/**
|
/**
|
||||||
* The resource that was is out
|
* The resource that was is out
|
||||||
*
|
*
|
||||||
* @return Illuminate\Database\Eloquent\Relations\MorphTo
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||||
*/
|
*/
|
||||||
public function checkoutable()
|
public function checkoutable()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue