How do I make an alert near the return date?

1

I'm developing a library system for an institution, but I'm having trouble alerting when the return date is near or leaving the fields in red when a student does not deliver the book on a given day.

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Emprestimo;
use App\Livro;
use App\Aluno;
use App\Usuario;


class EmprestimoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $registros = Emprestimo::with('livros','usuarios','alunos')->get();;
        return view('emprestimo.index', compact('registros'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $camposValidados = $request->validate([
            'dt_emprestimo' => 'required',
            'dt_devolucao'  => 'required', 
            'livro_id' => 'required',
            'aluno_id' => 'required',
    ]);

        $emprestimo = $request->user()->emprestimos()->create($camposValidados);
        $emprestimo->livros()->decrement('disponibilidade');


        return redirect()->to('emprestimo'); 
    }

    public function cadastrar()
    {
        $livros  = Livro::all();
        $usuarios  = Usuario::all();
        $alunos  = Aluno::all();

        return view('emprestimo.cadastrar', compact('livros','usuarios','alunos'));

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function editar($id)
    {   
            $registro = Emprestimo::where('id', '=', $id)->first();
            $livros = Livro::all();
            $usuarios = Usuario::all();
            $alunos = Aluno::all();
            return view('emprestimo.editar', compact('registro','livros','usuarios', 'alunos'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $camposValidados = $request->validate([
            'dt_emprestimo' => 'required',
            'dt_devolucao'  => 'required', 
            'livro_id' => 'required',
            'usuario_id' => 'required',
            'aluno_id' => 'required',

        ]);

        $emprestimo = Emprestimo::find($id);
        $emprestimo->fill($camposValidados);
        $emprestimo->save();

        return redirect()->to('emprestimo');


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $msgEmprestimo = "";


        if ($id) {
         Emprestimo::find($id)->delete();
         return redirect()->to('emprestimo')->with('sucesso', 'Emprestimo excluído com sucesso!');
         }

      return redirect()->route('emprestimo');
       }

       function CalcularVencimento($data,$dias)
       {
           $novadata = explode("/",$data);
           $dia = $novadata[0];
           $mes = $novadata[1];
           $ano = $novadata[2];
           //PARA DESCOBRIR QUAL DATA SERÁ DAQUI A 5 DIAS
           //echo date('d/m/Y',mktime(0,0,0,$mes,$dia+5,$ano));
           //PARA DESCOBRIR QUAL SERÁ O DIA AMANHÃ
           //echo date('d/m/Y',mktime(0,0,0,$mes,$dia+1,$ano));
           //PARA MÊS QUE VEM
           //echo date('d/m/Y',mktime(0,0,0,$mes + 1,$dia,$ano));
           //PARA ANO QUE VEM
           //echo date('d/m/Y',mktime(0,0,0,$mes,$dia,$ano + 1));
           if ($dias==0)
           {return date('d/m/Y',mktime(0,0,0,$mes,$dia,$ano));}
           else
           {return date('d/m/Y',mktime(0,0,0,$mes,$dia+$dias,$ano));}
       }
    }

Page

@extends('welcome')

@section('content')
<div class="container-fluid">
        <div class="box-header with-border">
            <h3 class="box-title">Lista de Emprestimo</h3>
        </div>
        <section class="content-header">
            <div>
                <ol class="breadcrumb">
                    <li><a href="bem-vindo">Home</a></li>
                    <li>Lista de Emprestimo</li>
                </ol>
            </div>
        </section>
        @include('flash')
        <div class="table-responsive">
            <table id="datatable" class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Data do Emprestimo</th>
                        <th>Data da Devolução</th>
                        <th>Livro</th>
                        <th>Usuario</th>
                        <th>Aluno</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($registros as $registro)
                        <tr>
                            <td>{{ $registro->id }}</td>
                            <td>{{ optional($registro->dt_emprestimo)->format('d/m/Y') }}</td>
                            <td>{{ optional($registro->dt_devolucao)->format('d/m/Y') }}</td>
                            <td>{{ optional($registro->livros)->nome }}</td>
                            <td>{{ optional($registro->usuarios)->nome }}</td>
                            <td>{{ optional($registro->alunos)->nome }}</td>


                            <td>
                                <a class="btn btn-warning btn-xs" title="Clique para editar!" href="{{route('emprestimo.editar', $registro->id)}}"><i class="glyphicon glyphicon-pencil"></i></a>
                                <a class="btn btn-danger btn-xs" title="Clique para excluir!" href="{{route('emprestimo.deletar', $registro->id)}}"><i class="glyphicon glyphicon-trash" onclick="if(!confirm('Você Deseja Excluir')) { return false; }"></i></a>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <div class="box-footer">
            <a class="btn btn-primary"  href="{{route('emprestimo.cadastrar')}}">
                <i class="fa fa-plus" aria-hidden="true"></i>           
                <span>Adicionar</span>
            </a>
        </div>
    </div>
    @endsection
    
asked by anonymous 15.03.2018 / 06:47

1 answer

2

As Laravel, by default, uses Carbon to work with dates, you can simply add x days and check if this new date equals the current date, for example:

$alert = $registro->dt_devolucao->between(\Carbon\Carbon::now(), \Carbon\Carbon::now()->subDays(5))

This code checks if the return date is between the current date and the current date - 5 days . With this code you can apply a if and depending on the condition, add an alert class in the tr element.

Full example:

@foreach($registros as $registro)

    @php
    $alert = $registro->dt_devolucao->between(\Carbon\Carbon::now(), \Carbon\Carbon::now()->subDays(5))
    @endphp

    <tr style="{{ $alert ? 'background:red' : 'background:green' }}">
        <td>{{ $registro->id }}</td>
        <td>{{ optional($registro->dt_emprestimo)->format('d/m/Y') }}</td>
        <td>{{ optional($registro->dt_devolucao)->format('d/m/Y') }}</td>
        <td>{{ optional($registro->livros)->nome }}</td>
        <td>{{ optional($registro->usuarios)->nome }}</td>
        <td>{{ optional($registro->alunos)->nome }}</td>


        <td>
            <a class="btn btn-warning btn-xs" title="Clique para editar!" href="{{route('emprestimo.editar', $registro->id)}}"><i class="glyphicon glyphicon-pencil"></i></a>
            <a class="btn btn-danger btn-xs" title="Clique para excluir!" href="{{route('emprestimo.deletar', $registro->id)}}"><i class="glyphicon glyphicon-trash" onclick="if(!confirm('Você Deseja Excluir')) { return false; }"></i></a>
        </td>
    </tr>
@endforeach
    
15.03.2018 / 12:51