At least in Tinker this seems to basically have all of the right relationships

This commit is contained in:
Brady Wetherington 2024-08-07 16:42:36 +01:00
parent b917489f00
commit 6f6a5f5412
4 changed files with 16 additions and 5 deletions

View file

@ -255,8 +255,7 @@ class Accessory extends SnipeModel
*/ */
public function checkouts() public function checkouts()
{ {
return $this->hasMany(\App\Models\AccessoryCheckout::class, 'accessory_id') return $this->hasMany(\App\Models\AccessoryCheckout::class, 'accessory_id'); // TODO - is that right, is that what I want?
->with('assignedTo');
} }
/** /**

View file

@ -45,7 +45,7 @@ class AccessoryCheckout extends Model
* @return \Illuminate\Database\Eloquent\Relations\Relation * @return \Illuminate\Database\Eloquent\Relations\Relation
*/ */
public function user() public function user()
{ { // This needs fixing
return $this->hasOne(\App\Models\User::class, 'user_id'); return $this->hasOne(\App\Models\User::class, 'user_id');
} }
@ -58,7 +58,7 @@ class AccessoryCheckout extends Model
*/ */
public function assignedTo() public function assignedTo()
{ {
return $this->morphTo('assigned', 'assigned_type', 'assigned_to')->withTrashed(); return $this->morphTo('assigned' /* this? */, 'assigned_type', 'assigned_to')->withTrashed();
} }
/** /**

View file

@ -249,10 +249,15 @@ class Location extends SnipeModel
* @return \Illuminate\Database\Eloquent\Relations\Relation * @return \Illuminate\Database\Eloquent\Relations\Relation
*/ */
public function assignedAssets() public function assignedAssets()
{ { //the morph relationship is weird here?
return $this->morphMany(\App\Models\Asset::class, 'assigned', 'assigned_type', 'assigned_to')->withTrashed(); return $this->morphMany(\App\Models\Asset::class, 'assigned', 'assigned_type', 'assigned_to')->withTrashed();
} }
public function assignedAccessories()
{
return $this->morphMany(AccessoryCheckout::class, 'assigned', 'assigned_type', 'assigned_to');
}
public function setLdapOuAttribute($ldap_ou) public function setLdapOuAttribute($ldap_ou)
{ {
return $this->attributes['ldap_ou'] = empty($ldap_ou) ? null : $ldap_ou; return $this->attributes['ldap_ou'] = empty($ldap_ou) ? null : $ldap_ou;

View file

@ -331,6 +331,13 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
*/ */
public function accessories() public function accessories()
{ {
/*******************************
*
* so 'assignedTo' as the name returns 'unknown column accessories_checkout.assignedTo_type'
* 'assigned' returns 'unknown column accessories_checkout.assigned_id'
*/
return $this->morphMany(AccessoryCheckout::class, 'assigned', 'assigned_type', 'assigned_to');
//no, wrong. I mean, it's skipping the polymorphic-ness
return $this->belongsToMany(\App\Models\Accessory::class, 'accessories_checkout', 'assigned_to', 'accessory_id') return $this->belongsToMany(\App\Models\Accessory::class, 'accessories_checkout', 'assigned_to', 'accessory_id')
->withPivot('id', 'created_at', 'note')->withTrashed()->orderBy('accessory_id'); ->withPivot('id', 'created_at', 'note')->withTrashed()->orderBy('accessory_id');
} }