From 6097d534e98d6fd00adc84686495ec9b2843af71 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 13 Apr 2023 11:10:16 -0700 Subject: [PATCH] Added eloquent model relationships Signed-off-by: snipe --- app/Models/Component.php | 30 +++++++++++++++++++++++++++++- app/Models/Consumable.php | 27 +++++++++++++++++++++++++++ app/Models/Supplier.php | 24 ++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/app/Models/Component.php b/app/Models/Component.php index 98230132be..052ec1219d 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -33,7 +33,8 @@ class Component extends SnipeModel 'name' => 'required|min:3|max:255', 'qty' => 'required|integer|min:1', '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', 'purchase_date' => 'date_format:Y-m-d|nullable', 'purchase_cost' => 'numeric|nullable|gte:0', @@ -57,6 +58,7 @@ class Component extends SnipeModel protected $fillable = [ 'category_id', 'company_id', + 'supplier_id', 'location_id', 'name', 'purchase_cost', @@ -86,6 +88,7 @@ class Component extends SnipeModel 'category' => ['name'], 'company' => ['name'], 'location' => ['name'], + 'supplier' => ['name'], ]; @@ -168,6 +171,18 @@ class Component extends SnipeModel return $this->belongsTo(\App\Models\Category::class, 'category_id'); } + /** + * Establishes the item -> supplier relationship + * + * @author [A. Gianotto] [] + * @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 * @@ -247,4 +262,17 @@ class Component extends SnipeModel { 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); + } } diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php index ea4ac6086b..a3a0d59178 100644 --- a/app/Models/Consumable.php +++ b/app/Models/Consumable.php @@ -27,6 +27,7 @@ class Consumable extends SnipeModel 'requestable' => 'boolean', 'category_id' => 'integer', 'company_id' => 'integer', + 'supplier_id', 'qty' => 'integer', 'min_amt' => 'integer', ]; @@ -95,6 +96,7 @@ class Consumable extends SnipeModel 'company' => ['name'], 'location' => ['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(); } + /** + * Establishes the item -> supplier relationship + * + * @author [A. Gianotto] [] + * @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 @@ -376,4 +390,17 @@ class Consumable extends SnipeModel { 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); + } } diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index 393e7ddb29..d60d3c42fe 100755 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -121,6 +121,30 @@ class Supplier extends SnipeModel return $this->hasMany(\App\Models\Accessory::class, 'supplier_id'); } + /** + * Establishes the supplier -> component relationship + * + * @author A. Gianotto + * @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 + * @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 *