pedi3/app/Models/Pedido.php

52 lines
1.2 KiB
PHP
Raw Normal View History

2024-03-11 19:41:52 -03:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-03-11 19:41:52 -03:00
use Illuminate\Database\Eloquent\Model;
class Pedido extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
/**
* El barrio al que pertenece el pedido.
*/
public function barrio(): BelongsTo
{
return $this->belongsTo(Barrio::class);
}
/**
* Los productos que pertenecen al pedido.
*/
public function productos(): BelongsToMany
{
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);
}
2024-03-11 19:41:52 -03:00
}