Besoin d'aide ?

mettre à jour (update à la place de créer une nouvelle entrée)

Avatar de AlexandreCarignan
AlexandreCarignan

Bonjour,

j'ai réussi à créer et enregistre de nouvelles données pour une nouvelle ville avec un store.

Maintenant j'ai créé une nouvelle page pour éditer le contenu déjà présent avec la function update. Cependant, aulieu de mettre à jour les données de la ville existante, je créer une nouvelle ville.

Est-ce que vous pourriez me guider pour réssoudre ce problème?

Merci d'avance. Alex.

Voici mon contrôleur EditableCityController.php

<?php

namespace App\Http\Controllers\Editable;

use App\Http\Controllers\Controller;
use App\Models\City;
use Illuminate\Http\Request;

class EditableCityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $cities = City::all();
        // tu sélectionnes toutes les entrées de la table 'cities'
        return view('editable.ville.index')->with('cities', $cities);

    }

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



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
            City::create($request->all());

            return redirect()->route('editor.user.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\models\city  $city
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return view('editable.ville.show', [
            'ville' => City::findOrFail($id)
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\models\city  $city
     * @return \Illuminate\Http\Response
     * if (Gate::denies('edit-users')) {
     *       return redirect()->route('home');
    *  }
     *
     */
    public function edit($id)
    {

        return view('editable.ville.edit', [

            'ville' => City::findOrFail($id)
        ]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\models\city  $city
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, City $ville)
    {
        $ville->name = $request->name;
        $ville->year = $request->year;
        $ville->house = $request->house;
        $ville->yearfoundation = $request->yearfoundation;
        $ville->size = $request->size;
        $ville->weather = $request->weather;
        $ville->accent1 = $request->accent1;
        $ville->accent2 = $request->accent2;
        $ville->localSpeciality = $request->localSpeciality;
        $ville->mayor = $request->mayor;
        $ville->urbanPopulaiton = $request->urbanPopulaiton;
        $ville->ruralPopulaiton = $request->ruralPopulaiton;
        $ville->tradeMax = $request->tradeMax;
        $ville->localDrink = $request->localDrink;
        $ville->education = $request->education;
        $ville->fortification = $request->fortification;
        $ville->lawAndOrder = $request->lawAndOrder;
        $ville->technology = $request->technology;
        $ville->wealth = $request->wealth;
        $ville->economy1 = $request->economy1;
        $ville->economy2 = $request->economy2;
        $ville->economy3 = $request->economy3;
        $ville->economy4 = $request->economy4;
        $ville->economy5 = $request->economy5;
        $ville->economy6 = $request->economy6;
        $ville->economy7 = $request->economy7;
        $ville->economy8 = $request->economy8;
        $ville->economy9 = $request->economy9;
        $ville->economy10 = $request->economy10;
        $ville->offer1 = $request->offer1;
        $ville->offer2 = $request->offer2;
        $ville->nextCity1 = $request->nextCity1;
        $ville->nextCity2 = $request->nextCity2;
        $ville->offer1NextCity1 = $request->offer1NextCity1;
        $ville->offer2NextCity1 = $request->offer2NextCity1;
        $ville->offer1NextCity2 = $request->offer1NextCity2;
        $ville->offer2NextCity2 = $request->offer2NextCity2;
        $ville->demand1 = $request->demand1;
        $ville->demand2 = $request->demand2;
        $ville->demand1NextCity1 = $request->demand1NextCity1;
        $ville->demand2NextCity1 = $request->demand2NextCity1;
        $ville->demand1NextCity2 = $request->demand1NextCity2;
        $ville->demand2NextCity2 = $request->demand2NextCity2;
        $ville->story = $request->story;
        $ville->version = $request->version;
        $ville->chapter = $request->chapter;

        $ville->save();

        return redirect()->route('editor.user.index');

    }








    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\models\city  $city
     * @return \Illuminate\Http\Response
     */
    public function destroy(city $city)
    {
        //
    }
}


Voici ma page qui contient mon formulaire. Edit.blade.php

@extends('layouts.app')
@section('title')
Mise à jour | {{ $ville->name }}
@endsection

@if(session()->has('info'))
    <div class="notification is-success">
        {{ session('info') }}
    </div>
@endif


@section('content')

    <div class="row justify-content-center pt-4">
        <div class="col-md-6">
            <div class="card">


                    <div class="card-header p-4">
                        <div class="row justify-content-center">
                          <p><h2><strong>{{ __('Modifier') }} {{ $ville->name }}</strong></h2></p>
                        </div>


                    </div>



                    <div class="card-body p-4">


                        <form class="form-floating" action="{{ route('ville.city.update', $ville->id) }}" method="POST">
                            @csrf
                            {{ method_field('PATCH') }}

                            <!-- EditForm -->
                                @include('editable.ville.partials.editForm')
                            <!-- end: EditForm -->

                        </form>

                    </div>

                </div>

            </div>
        </div>

    </div>

@endsection

voici mon show.blade.php qui redirige vers le edit.

<div class="card-header">
                        <div class="row justify-content-center pt-4">
                            <p><h1><strong>{{ $ville->name }}</strong></h1><a href="{{ route('ville.city.edit', $ville->id) }}">( modifier )</a></p>
                        </div>
                        <div>
                            <p>En l’an {{ $ville->year }}</p>
                        </div>
 </div>

Posté il y a 3 mois
Avatar de AlexandreCarignan
AlexandreCarignan

J'ai trouvé la solution pour l'update:

public function update(Request $request, $id) {

    $ville = $request->all();
    $ville = City::find($id);
    $ville->update($request->all());

    return redirect()->route('editor.user.index');

}
Posté il y a 3 mois

Vous ne pouvez pas répondre à ce sujet.