我刚刚开始使用Laravel,并在表格单元格中,在索引视图中,我需要使用当前日期作为参考来显示直到产品到期需要多少天。在我的数据库中,我已经存储了失效日期。
我的数据库表中没有字段来存储天差。我只是在视图中显示结果。
我正在使用的Carbon版本为2.16
我已经从控制器获得了天差。但是当我存储更多值时,所有DaysDiff字段都将替换为上次存储日期的结果。
我的控制器
use Carbon\Carbon;
public function index()
{
$reservs = ReservasModelo::get();
$test = ReservasModelo::orderby('FechavencR')->pluck('FechavencR');
foreach ($test as $tes) {
$difer = Carbon::now();
$fini = $difer->diffInDays($tes);
}
return view('reservas.index', compact('reservs','fini'));
}
我的观点
<tbody>
@foreach($reservs as $reserv)
<tr>
<td class="text-center">{{ $reserv->idReservas}}</td>
<td class="text-center">{{ $reserv->FechavencR}}</td>
<td class="text-center">{{ $fini}}<td>
<td class="text-center">{{ $reserv->UnidadMedida}}</td>
<td class="text-center">{{ $reserv->Racion}}</td>
<td class="text-center">{{ $reserv->RacionMes}}</td>
<td class="text-center">{{ $reserv->CantNece}}</td>
<td class="text-center">{{ $reserv->Existencia}}</td>
<td class="text-center">{{ $reserv->Variac}}</td>
<td class="text-center">{{ $reserv->idInventariosR}}</td>
<td class="text-center">{{ $reserv->idSucursalesR}}</td>
<td class="d-flex">
@csrf
<a href="{{ route('reservas.show', $reserv->idReservas)}}" class="btn btn-info">Ver</a>
<a href="{{ route('reservas.edit', $reserv->idReservas)}}" class="btn btn-primary">Editar</a>
<form action="{{ route('reservas.destroy', $reserv->idReservas)}}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Eliminar</button>
</form>
</td>
</tr>
@endforeach
</tbody>
我的数据库表
reservas = [idReservas,FechavencR,UnidadMedida,Racion,RacionMes,CantNece,Existencia,Variac,idInventariosR,idSucursalesR] << / p>
我想在表格中显示与各自有效日期的天差。
对不起,英语不好。
答案 0 :(得分:0)
public function index()
{
$reservs = ReservasModelo::get()->toArray();
$test = ReservasModelo::orderby('FechavencR')->pluck('FechavencR');
foreach ($test as $key=>$tes) {
$difer = Carbon::now();
$reservs[$key]['fini'] = $difer->diffInDays($tes);
}
return view('reservas.index', compact('reservs'));
}
@foreach($reservs as $reserv)
<tr>
<td class="text-center">{{ $reserv['idReservas'] }}</td>
<td class="text-center">{{ $reserv['FechavencR'] }}</td>
<td class="text-center">{{ $reserv['fini'] }}<td>
<td class="text-center">{{ $reserv['UnidadMedida'] }}</td>
<td class="text-center">{{ $reserv['Racion'] }}</td>
<td class="text-center">{{ $reserv['RacionMes'] }}</td>
<td class="text-center">{{ $reserv['CantNece'] }}</td>
<td class="text-center">{{ $reserv['Existencia']}}</td>
<td class="text-center">{{ $reserv['Variac'] }}</td>
<td class="text-center">{{ $reserv['idInventariosR'] }}</td>
<td class="text-center">{{ $reserv['idSucursalesR'] }}</td>
<td class="d-flex">
@csrf
<a href="{{ route('reservas.show', $reserv['idReservas'])}}" class="btn btn-info">Ver</a>
<a href="{{ route('reservas.edit', $reserv['idReservas'])}}" class="btn btn-primary">Editar</a>
<form action="{{ route('reservas.destroy', $reserv['idReservas'])}}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Eliminar</button>
</form>
</td>
</tr>
@endforeach
答案 1 :(得分:0)
我也找到了这种方法。
型号
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class ReservasModelo extends Model
{
protected $table='reservas';
protected $primaryKey='idReservas';
protected $fillable = array ('idReservas','FechavencR','UnidadMedida','Racion','RacionMes','CantNece','Existencia','Variac','idInventariosR','idSucursalesR');
public function vencidos()
{
$difer = Carbon::now();
$fini = $difer->diffInDays($this->FechavencR,false);
return $fini;
}
}
查看
<tbody>
@foreach($reservs as $reserv)
<tr>
<td class="text-center">{{ $reserv->idReservas}}</td>
<td class="text-center">{{ $reserv->idInventariosR}}</td>
<td class="text-center">{{ $reserv->idSucursalesR}}</td>
<td class="text-center">{{ $reserv->FechavencR}}</td>
<td class="text-center">{{ $reserv->UnidadMedida}}</td>
<td class="text-center">{{ $reserv->Racion}}</td>
<td class="text-center">{{ $reserv->RacionMes}}</td>
<td class="text-center">{{ $reserv->CantNece}}</td>
<td class="text-center">{{ $reserv->Existencia}}</td>
<td class="text-center">{{ $reserv->Variac}}</td>
<td class="text-center">{{ $reserv->vencidos()}}</td>
<td class="d-flex">
@if(auth()->user()->tipo!='UsuarioBasico')
@csrf
<a href="{{ route('reservas.show', $reserv->idReservas)}}" class="btn btn-info btn-sm">Ver</a>
<a href="{{ route('reservas.edit', $reserv->idReservas)}}" class="btn btn-primary btn-sm">Editar</a>
<form action="{{ route('reservas.destroy', $reserv->idReservas)}}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger btn-sm" type="submit">Eliminar</button>
</form>
@endif
</td>
</tr>
@endforeach
</tbody>