Besoin d'aide ?

Adapter ma method store() pour un update()

Avatar de Soufiane1
Soufiane1

Bonsoir,

J'ai dans mon formulaire **payment **un champ nommé date_payment avec comme valeur 15/10/2019.

Ensuite, dans mon formulaire training, j'ai un champ nommé date_seance, avec la valeur 25/10/2019.

Admettons, qu'on veuille changer la valeur de la date_seance par 10/10/2019, la date_seance est plus petite que la date_payment.

J'aimerais, donc ajouter un message d'erreur qui stipule que la date de l'entrainement est inférieur à la date de paiement.

Ma method store() est OK

$datePayment = Payment::where('fk_student', $request->get('fk_student'))
->whereDate('date_payment', ">" , $date_seance)
->first();
 if(isset($datePayment)){
            return redirect()->route('trainings.index')
                ->with('error', 'There is a problem with the payment date! ');
}

Je suis bloqué pour la méthode update(), je ne vois pas trop comment faire ça ?? :-(

Posté il y a 9 mois
Avatar de Dom
Dom

Pourquoi ne pas utiliser les FormRequest ? https://laravel.com/docs/6.x/validation

Je ne vois pas où tu es bloqué, tu peux mettre l'ensemble de ton controller ?

Posté il y a 9 mois
Avatar de Soufiane1
Soufiane1

Bonjour Dom,

Je bloque dans ma methode update(), je n'arrive pas à comparer correctement la date_payment avec la date_seance. Comme dans ma methode store() qui est correcte.

<?php

namespace App\Http\Controllers;
use App\Motorbike;
use App\Former;
use App\Training;
use App\Revision;
use App\Student; 
use App\Payment; 
use App\Typeseance; 


use Illuminate\Http\Request;

class TrainingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    public function index()
    {
        $trainings = Training::oldest()->paginate(5);
        return view('admin.trainings.index', compact('trainings'));
                with('i', (request()->input('page', 1) -1) *5);
    }

 

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        $motorbikes = Motorbike::all();
        $formers = Former::all();
        $students = Student::all();
        $typeseances = Typeseance::all();
        return view('admin.trainings.create', compact('motorbikes','formers', 'students', 'typeseances', 'trainings'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_motorbike' => 'required',
                'fk_former' => 'required',
                'fk_student' => 'required',
                'fk_typeseance' => 'required'
                
                
        ]);


       $date_seance = $request->get('date_seance'); 
       $hour_start = $request->get('hour_start'); 
       $hour_end = $request->get('hour_end'); 
       $fk_motorbike = $request->get('fk_motorbike');
       $fk_student = $request->get('fk_student');
       $fk_former = $request->get('fk_former');
       $fk_typeseance = $request->get('fk_typeseance');



        $datePayment = Payment::where('fk_student', $request->get('fk_student'))
        ->whereDate('date_payment', ">" , $date_seance)
        ->first();

        if(isset($datePayment)){
            return redirect()->route('trainings.index')
            ->with('error', 'There is a problem with the payment date! ');
        }
        else{
            Training::create($request->all());
                return redirect()->route('trainings.index')
                    ->with('success', 'Add');
        }

       

    }

   
    public function edit($id)
    {
        $trainings = Training::find($id);
        $students = Student::all();
        $formers = Former::all();
        $motorbikes = Motorbike::all();
        $payments = Payment::all();

        return view('admin.trainings.edit', compact('trainings', 'students', 'formers', 'motorbikes', 'payments'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_motorbike' => 'required',
                'fk_former' => 'required',
                'fk_student' => 'required'
        ]);

       $date_seance = $request->get('date_seance'); 
       $hour_start = $request->get('hour_start'); 
       $hour_end = $request->get('hour_end'); 
       $fk_motorbike = $request->get('fk_motorbike');
       $fk_student = $request->get('fk_student');
       $fk_former = $request->get('fk_former');
       $fk_typeseance = $request->get('fk_typeseance');



        $datePayment = Payment::where('fk_student', $request->get('fk_student'))
        ->whereDate('date_payment', ">" , $date_seance)
        ->first();



        $trainings = Training::find($id);
        $trainings->date_seance = $request->get('date_seance');
        $trainings->hour_start = $request->get('hour_start');
        $trainings->hour_end = $request->get('hour_end');
        $trainings->fk_motorbike = $request->get('fk_motorbike');
        $trainings->fk_former = $request->get('fk_former');
        $trainings->fk_student = $request->get('fk_student');
        $trainings->save();
        return redirect()->route('trainings.index')
            ->with('success', 'Update!')->withInput();
     
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $trainings = Training::find($id);
        $trainings->delete();
        return redirect()->route('trainings.index')
                    ->with('error', 'Delete! ');
    }






}

PS: Pour ce type d'exercice, je n'ai pas besoin du formRequest, mais je l'utiliserai à l'avenir.

Posté il y a 9 mois

Vous ne pouvez pas répondre à ce sujet.