Eager loading assignedTo - I have no idea why this works

This commit is contained in:
snipe 2017-10-28 03:50:02 -07:00
parent 890012f6c4
commit 5278dac2b0
2 changed files with 11 additions and 11 deletions

View file

@ -85,7 +85,7 @@ class AssetsController extends Controller
} }
$assets = Company::scopeCompanyables(Asset::select('assets.*'))->with( $assets = Company::scopeCompanyables(Asset::select('assets.*'))->with(
'location', 'assetstatus', 'assetlog', 'company', 'defaultLoc', 'location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo',
'model.category', 'model.manufacturer', 'model.fieldset','supplier'); 'model.category', 'model.manufacturer', 'model.fieldset','supplier');

View file

@ -130,19 +130,19 @@ class AssetsTransformer
public function transformAssignedTo($asset) public function transformAssignedTo($asset)
{ {
if ($asset->checkedOutToUser()) { if ($asset->checkedOutToUser()) {
return $asset->assignedTo ? [ return $asset->assigned ? [
'id' => (int) $asset->assignedTo->id, 'id' => (int) $asset->assigned->id,
'username' => e($asset->assignedTo->username), 'username' => e($asset->assigned->username),
'name' => e($asset->assignedTo->getFullNameAttribute()), 'name' => e($asset->assigned->getFullNameAttribute()),
'first_name'=> e($asset->assignedTo->first_name), 'first_name'=> e($asset->assigned->first_name),
'last_name'=> e($asset->assignedTo->last_name), 'last_name'=> e($asset->assigned->last_name),
'employee_number' => e($asset->assignedTo->employee_num), 'employee_number' => e($asset->assigned->employee_num),
'type' => 'user' 'type' => 'user'
] : null; ] : null;
} }
return $asset->assignedTo ? [ return $asset->assigned ? [
'id' => $asset->assignedTo->id, 'id' => $asset->assigned->id,
'name' => $asset->assignedTo->display_name, 'name' => $asset->assigned->display_name,
'type' => $asset->assignedType() 'type' => $asset->assignedType()
] : null; ] : null;
} }