imports arreglados y relaciones agregadas

This commit is contained in:
Alejandro Tasistro 2024-03-19 15:40:27 -03:00
parent 382b973227
commit a569a2a497
6 changed files with 27 additions and 8 deletions

View File

@ -2,7 +2,8 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Barrio extends Model class Barrio extends Model

View File

@ -2,7 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Caracteristica extends Model class Caracteristica extends Model

View File

@ -2,7 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Categoria extends Model class Categoria extends Model

View File

@ -2,7 +2,8 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Pedido extends Model class Pedido extends Model
@ -29,6 +30,22 @@ public function barrio(): BelongsTo
*/ */
public function productos(): BelongsToMany public function productos(): BelongsToMany
{ {
return $this->belongsToMany(Producto::class)->withPivot(['cantidad','total']); return $this->belongsToMany(Producto::class)->withPivot(['cantidad']);
}
public function productosSinBonos() {
return $this->productos()->where('bono',0)->all();
}
function bonos() {
return $this->productos()->where('bono',1)->all();
}
function bonosDeTransporte() : int {
$total = 0;
foreach ($this->productosSinBonos() as $key => $producto) {
$total += $producto->price * $producto->pivot->cantidad;
}
return 1 + ($total / 500);
} }
} }

View File

@ -2,7 +2,8 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Producto extends Model class Producto extends Model
@ -29,7 +30,7 @@ public function categoria(): BelongsTo
*/ */
public function pedidos(): BelongsToMany public function pedidos(): BelongsToMany
{ {
return $this->belongsToMany(Pedido::class)->withPivot(['cantidad','total']); return $this->belongsToMany(Pedido::class)->withPivot(['cantidad']);
} }
/** /**

View File

@ -2,7 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Region extends Model class Region extends Model