Added eloquent model relationships

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-04-13 11:10:16 -07:00
parent 3aac8bccd2
commit 6097d534e9
3 changed files with 80 additions and 1 deletions

View file

@ -33,7 +33,8 @@ class Component extends SnipeModel
'name' => 'required|min:3|max:255', 'name' => 'required|min:3|max:255',
'qty' => 'required|integer|min:1', 'qty' => 'required|integer|min:1',
'category_id' => 'required|integer|exists:categories,id', 'category_id' => 'required|integer|exists:categories,id',
'company_id' => 'integer|nullable', 'supplier_id' => 'nullable|integer|exists:suppliers,id',
'company_id' => 'integer|nullable|exists:companies,id',
'min_amt' => 'integer|min:0|nullable', 'min_amt' => 'integer|min:0|nullable',
'purchase_date' => 'date_format:Y-m-d|nullable', 'purchase_date' => 'date_format:Y-m-d|nullable',
'purchase_cost' => 'numeric|nullable|gte:0', 'purchase_cost' => 'numeric|nullable|gte:0',
@ -57,6 +58,7 @@ class Component extends SnipeModel
protected $fillable = [ protected $fillable = [
'category_id', 'category_id',
'company_id', 'company_id',
'supplier_id',
'location_id', 'location_id',
'name', 'name',
'purchase_cost', 'purchase_cost',
@ -86,6 +88,7 @@ class Component extends SnipeModel
'category' => ['name'], 'category' => ['name'],
'company' => ['name'], 'company' => ['name'],
'location' => ['name'], 'location' => ['name'],
'supplier' => ['name'],
]; ];
@ -168,6 +171,18 @@ class Component extends SnipeModel
return $this->belongsTo(\App\Models\Category::class, 'category_id'); return $this->belongsTo(\App\Models\Category::class, 'category_id');
} }
/**
* Establishes the item -> supplier relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v6.1.1]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function supplier()
{
return $this->belongsTo(\App\Models\Supplier::class, 'supplier_id');
}
/** /**
* Establishes the component -> action logs relationship * Establishes the component -> action logs relationship
* *
@ -247,4 +262,17 @@ class Component extends SnipeModel
{ {
return $query->leftJoin('companies', 'components.company_id', '=', 'companies.id')->orderBy('companies.name', $order); return $query->leftJoin('companies', 'components.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
} }
/**
* Query builder scope to order on supplier
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order
*
* @return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderSupplier($query, $order)
{
return $query->leftJoin('suppliers', 'components.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order);
}
} }

View file

@ -27,6 +27,7 @@ class Consumable extends SnipeModel
'requestable' => 'boolean', 'requestable' => 'boolean',
'category_id' => 'integer', 'category_id' => 'integer',
'company_id' => 'integer', 'company_id' => 'integer',
'supplier_id',
'qty' => 'integer', 'qty' => 'integer',
'min_amt' => 'integer', 'min_amt' => 'integer',
]; ];
@ -95,6 +96,7 @@ class Consumable extends SnipeModel
'company' => ['name'], 'company' => ['name'],
'location' => ['name'], 'location' => ['name'],
'manufacturer' => ['name'], 'manufacturer' => ['name'],
'supplier' => ['name'],
]; ];
@ -249,6 +251,18 @@ class Consumable extends SnipeModel
return $this->belongsToMany(\App\Models\User::class, 'consumables_users', 'consumable_id', 'assigned_to')->withPivot('user_id')->withTrashed()->withTimestamps(); return $this->belongsToMany(\App\Models\User::class, 'consumables_users', 'consumable_id', 'assigned_to')->withPivot('user_id')->withTrashed()->withTimestamps();
} }
/**
* Establishes the item -> supplier relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v6.1.1]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function supplier()
{
return $this->belongsTo(\App\Models\Supplier::class, 'supplier_id');
}
/** /**
* Determine whether to send a checkin/checkout email based on * Determine whether to send a checkin/checkout email based on
@ -376,4 +390,17 @@ class Consumable extends SnipeModel
{ {
return $query->leftJoin('companies', 'consumables.company_id', '=', 'companies.id')->orderBy('companies.name', $order); return $query->leftJoin('companies', 'consumables.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
} }
/**
* Query builder scope to order on supplier
*
* @param \Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order
*
* @return \Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderSupplier($query, $order)
{
return $query->leftJoin('suppliers', 'consumables.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order);
}
} }

View file

@ -121,6 +121,30 @@ class Supplier extends SnipeModel
return $this->hasMany(\App\Models\Accessory::class, 'supplier_id'); return $this->hasMany(\App\Models\Accessory::class, 'supplier_id');
} }
/**
* Establishes the supplier -> component relationship
*
* @author A. Gianotto <snipe@snipe.net>
* @since [v6.1.1]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function components()
{
return $this->hasMany(\App\Models\Component::class, 'supplier_id');
}
/**
* Establishes the supplier -> component relationship
*
* @author A. Gianotto <snipe@snipe.net>
* @since [v6.1.1]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function consumables()
{
return $this->hasMany(\App\Models\Consumable::class, 'supplier_id');
}
/** /**
* Establishes the supplier -> asset maintenances relationship * Establishes the supplier -> asset maintenances relationship
* *