How to make an alert close to the return date?

Asked

Viewed 101 times

1

I am developing a library system for an institution, but I am having difficulty making an alert when the return date is near or leave the fields in red when a student does not deliver the book on the day could.

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
  • But what mistake happens? What would be "next" to the date?

1 answer

2


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

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

This code checks whether 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 to the element tr.

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
  • I put this example in the code and there appeared no warning still does not alert what I can do please ?

  • You need to create a CSS. Ex: .alert { background:red }

  • sorry keeps you getting bogged down with questions but you could make another example doing please ? why are you still starting to mess with java script

  • @Eduardoezequiel , edited

  • guy so your fan kkk thank you very much even you have some social network brother ?

  • In the my profile has some.

  • Just one more question I wanted to put to you that as within the deadline in green as I do ?

  • @Eduardoezequiel updated the example

  • thank you very much again .

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.