Besoin d'aide ?

General error: 1364 Field 'name' avec l'utilisation de public function store(Request $request)

Avatar de AlexandreCarignan
AlexandreCarignan

Bonjour après avoir suivi plusieurs cours et tutoriel, j'ai décidé de me pratiquer avec une application que j'ai longtemps rêvé.

Voici mon erreur lors de l'envoi du formulaire pour créer une nouvelle ville. Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into cities (updated_at, created_at) values (2020-12-30 21:26:59, 2020-12-30 21:26:59))

Ma route:

Route::namespace('Editable')->prefix('editable')->name('ville.')->group(function() {
    Route::resource('city', 'EditableCityController');
});

voici mon modèl City.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class City extends Model
{
    protected $fillable = [
        'name',
        'year',
        'house',
        'yearfoundation',
        'size',
        'weather',
        'accent1',
        'accent2',
        'localSpeciality',
        'mayor',
        'urbanPopulaiton',
        'ruralPopulaiton',
        'tradeMax',
        'localDrink',
        'education',
        'fortification',
        'lawAndOrder',
        'technology',
        'wealth',
        'economy1',
        'economy2',
        'economy3',
        'economy4',
        'economy5',
        'economy6',
        'economy7',
        'economy8',
        'economy9',
        'economy10',
        'offer1',
        'offer2',
        'nextCity1',
        'nextCity2',
        'offer1NextCity1',
        'offer2NextCity1',
        'offer1NextCity2',
        'offer2NextCity2',
        'demand1',
        'demand2',
        'demand1NextCity1',
        'demand2NextCity1',
        'demand1NextCity2',
        'demand2NextCity2',
        'story',
        'version',
        'chapter',
    ];
}

Mon controlleur: 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(Request $request)
    {
        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());
            $city->name = $request->get('name');
            $city->year = $request->get('year');
            $city->house = $request->get('house');
            $city->yearfoundation = $request->get('yearfoundation');
            $city->size = $request->get('size');
            $city->weather = $request->get('weather');
            $city->accent1 = $request->get('accent1');
            $city->accent2 = $request->get('accent2');
            $city->localSpeciality = $request->get('localSpeciality');
            $city->mayor = $request->get('mayor');
            $city->urbanPopulaiton = $request->get('urbanPopulaiton');
            $city->ruralPopulaiton = $request->get('ruralPopulaiton');
            $city->tradeMax = $request->get('tradeMax');
            $city->localDrink = $request->get('localDrink');
            $city->education = $request->get('education');
            $city->fortification = $request->get('fortification');
            $city->lawAndOrder = $request->get('lawAndOrder');
            $city->technology = $request->get('technology');
            $city->wealth = $request->get('wealth');
            $city->economy1 = $request->get('economy1');
            $city->economy2 = $request->get('economy2');
            $city->economy3 = $request->get('economy3');
            $city->economy4 = $request->get('economy4');
            $city->economy5 = $request->get('economy5');
            $city->economy6 = $request->get('economy6');
            $city->economy7 = $request->get('economy7');
            $city->economy8 = $request->get('economy8');
            $city->economy9 = $request->get('economy9');
            $city->economy10 = $request->get('economy10');
            $city->offer1 = $request->get('offer1');
            $city->offer2 = $request->get('offer2');
            $city->nextCity1 = $request->get('nextCity1');
            $city->nextCity2 = $request->get('nextCity2');
            $city->offer1NextCity1 = $request->get('offer1NextCity1');
            $city->offer2NextCity1 = $request->get('offer2NextCity1');
            $city->offer1NextCity2 = $request->get('offer1NextCity2');
            $city->offer2NextCity2 = $request->get('offer2NextCity2');
            $city->demand1 = $request->get('demand1');
            $city->demand2 = $request->get('demand2');
            $city->demand1NextCity1 = $request->get('demand1NextCity1');
            $city->demand2NextCity1 = $request->get('demand2NextCity1');
            $city->demand1NextCity2 = $request->get('demand1NextCity2');
            $city->demand2NextCity2 = $request->get('demand2NextCity2');
            $city->story = $request->get('story');
            $city->version = $request->get('version');
            $city->chapter = $request->get('chapter');
            
            return redirect()->route('profile.editor.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
     */
    public function edit(city $city)
    {
        //
    }

    /**
     * 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 $city)
    {
        //
    }

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

J'ai l'impression que les informations ne sont pas passés.

mon formulaire est dans le fichier /editable/ville/create.blade.php Ma ligne de formulaire est ceci:

<form class="form-floating" action="{{ route('ville.city.store') }}" method="POST">
                        @csrf

                            <div class="container border rounded p-4">

                                <div class="form-floating">
                                    <label for="year">{{__('En l’an :') }}</label>
                                    <input type="text" class="form-control" id="year" placeholder="" value="" autofocus>
                                </div>

                                <div class="form-floating">
                                    <label for="name">{{ __('Nom') }}</label>
                                    <input type="text" class="form-control" id="name" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="house">{{ __('Maison') }}</label>
                                    <input type="text" class="form-control" id="house" placeholder="" value="">
                                </div>

                            </div>

                            <div class="container border rounded p-4">

                                <div class="form-floating">
                                    <label for="yearfoundation">{{ __('Année de fondation') }}</label>
                                    <input type="text" class="form-control" id="yearfoundation" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="size">{{ __('Taille') }}</label>
                                        <select class="form-select" id="size" aria-label="Floating label select example">
                                        <option selected>Ville</option>
                                        <option value="Capitale">Capitale</option>
                                        <option value="Cité">Cité</option>
                                        <option value="Métropole">Métropole</option>
                                        <option value="Village">Village</option>
                                        <option value="Ville">Ville</option>
                                        </select>
                                </div>

                                <div class="form-floating pt-2">
                                    <label for="weather">{{ __('Climat') }}</label>
                                        <input type="text" class="form-control" id="weather" placeholder="" value="">
                                </div>

                                <div class="form-floating">

                                    <label for="accent1">{{ __('Accents régionales') }}</label>
                                    <div>
                                            <input type="text" class="form-control" id="accent1" placeholder="Accent 1, s'il n'existe pas" value="">
                                                <select class="form-select pt-2" id="accent1" aria-label="Floating label select example">
                                                    <option selected></option>
                                                        @foreach(App\Models\City:: get() as $ville)
                                                            <option value="{{ $ville->accent1 }}">{{ $ville->accent1}}</option>
                                                        @endforeach
                                                </select>
                                    </div>
                                    <div class="pt-2">
                                            <input type="text" class="form-control" id="accent2" placeholder="Accent 2, s'il n'existe pas" value="">
                                                <select class="form-select pt-2" id="accent2" aria-label="Floating label select example">
                                                    <option selected></option>
                                                        @foreach(App\Models\City:: get() as $ville)
                                                            <option value="{{ $ville->accent1 }}">{{ $ville->accent1 }}</option>
                                                        @endforeach
                                                </select>
                                    </div>
                                </div>


                                <div class="form-floating pt-2">
                                    <label for="localSpeciality">{{ __('Spécialité locale') }}</label>
                                        <input type="text" class="form-control" id="localSpeciality" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="mayor">{{ __('Maire ou mairesse') }}</label>
                                        <input type="text" class="form-control" id="mayor" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="urbanPopulaiton">{{ __('Population urbaine') }}</label>
                                        <input type="text" class="form-control" id="urbanPopulaiton" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="ruralPopulaiton">{{ __('Population régionale') }}</label>
                                        <input type="text" class="form-control" id="ruralPopulaiton" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="tradeMax">{{ __('Commerce maximal') }}</label>
                                        <input type="text" class="form-control" id="tradeMax" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="localDrink">{{ __('Boisson locale') }}</label>
                                        <input type="text" class="form-control" id="localDrink" placeholder="" value="">
                                </div>
                            </div>
                            <div class="container border rounded p-4">
                                <div class="form-floating">
                                    <label for="education">{{ __('Éducation') }}</label>
                                        <select class="form-select" id="education" aria-label="Floating label select example">
                                            <option selected></option>
                                            <option value="1">1</option>
                                            <option value="2">2</option>
                                            <option value="3">3</option>
                                            <option value="4">4</option>
                                            <option value="5">5</option>
                                            <option value="6">6</option>
                                            <option value="7">7</option>
                                            <option value="8">8</option>
                                            <option value="9">9</option>
                                            <option value="10">10</option>
                                        </select>
                                </div>

                                <div class="form-floating">
                                    <label for="fortification">{{ __('Fortification') }}</label>
                                        <select class="form-select" id="fortification" aria-label="Floating label select example">
                                            <option selected></option>
                                            <option value="1">1</option>
                                            <option value="2">2</option>
                                            <option value="3">3</option>
                                            <option value="4">4</option>
                                            <option value="5">5</option>
                                            <option value="6">6</option>
                                            <option value="7">7</option>
                                            <option value="8">8</option>
                                            <option value="9">9</option>
                                            <option value="10">10</option>
                                        </select>
                                </div>

                                <div class="form-floating">
                                    <label for="lawAndOrder">{{ __('Loi et l’ordre') }}</label>
                                        <select class="form-select" id="lawAndOrder" aria-label="Floating label select example">
                                            <option selected></option>
                                            <option value="1">1</option>
                                            <option value="2">2</option>
                                            <option value="3">3</option>
                                            <option value="4">4</option>
                                            <option value="5">5</option>
                                            <option value="6">6</option>
                                            <option value="7">7</option>
                                            <option value="8">8</option>
                                            <option value="9">9</option>
                                            <option value="10">10</option>
                                        </select>
                                </div>

                                <div class="form-floating">
                                    <label for="technology">{{ __('Technologie') }}</label>
                                        <select class="form-select" id="technology" aria-label="Floating label select example">
                                            <option selected></option>
                                            <option value="1">1</option>
                                            <option value="2">2</option>
                                            <option value="3">3</option>
                                            <option value="4">4</option>
                                            <option value="5">5</option>
                                            <option value="6">6</option>
                                            <option value="7">7</option>
                                            <option value="8">8</option>
                                            <option value="9">9</option>
                                            <option value="10">10</option>
                                        </select>
                                </div>

                                <div class="form-floating">
                                    <label for="wealth">{{ __('Richesse') }}</label>
                                        <select class="form-select" id="wealth" aria-label="Floating label select example">
                                            <option selected></option>
                                            <option value="1">1</option>
                                            <option value="2">2</option>
                                            <option value="3">3</option>
                                            <option value="4">4</option>
                                            <option value="5">5</option>
                                            <option value="6">6</option>
                                            <option value="7">7</option>
                                            <option value="8">8</option>
                                            <option value="9">9</option>
                                            <option value="10">10</option>
                                        </select>
                                </div>


                            </div>
                            <div class="container border rounded p-4">
                                <div class="form-floating">
                                    <label for="economy">{{ __('Économie') }}</label>
                                        <input type="text" class="form-control" id="economy1" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy2" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy3" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy4" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy5" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy6" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy7" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy8" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy9" placeholder="" value="">
                                        <input type="text" class="form-control" id="economy10" placeholder="" value="">
                                </div>
                            </div>

                            <div class="container border rounded p-4">

                                <div class="form-floating">
                                    <label for="offers">{{ __('Offres') }}</label>
                                        <input type="text" class="form-control" id="offer1" placeholder="" value="">
                                        <input type="text" class="form-control" id="offer2" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="demands">{{ __('Demandes') }}</label>
                                        <input type="text" class="form-control" id="demand1" placeholder="" value="">
                                        <input type="text" class="form-control" id="demand2" placeholder="" value="">
                                    </div>
                            </div>

                            <div class="container border rounded p-4">

                                <div class="form-floating">
                                    <label for="nextCity1">{{ __('Villes voisines 1') }}</label>
                                        <input type="text" class="form-control" id="nextCity1" placeholder="Si nouvelle ville" value="">
                                            <select class="form-select" id="nextCity1" aria-label="Floating label select example">
                                                <option selected></option>
                                                @foreach(App\Models\City:: get() as $ville)
                                                <option value="{{ $ville->id }}">{{ $ville->name }}</option>
                                                @endforeach
                                            </select>

                                </div>

                                <div class="form-floating pt-2">
                                    <label for="offerNextCity1">{{ __('Offres ville voisine 1') }}</label>
                                        <input type="text" class="form-control" id="offer1NextCity1" placeholder="" value="">
                                        <input type="text" class="form-control" id="offer2NextCity1" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="demandNextCity1">{{ __('Demandes ville voisine 1') }}</label>
                                        <input type="text" class="form-control" id="demand1NextCity1" placeholder="" value="">
                                        <input type="text" class="form-control" id="demand2NextCity1" placeholder="" value="">
                                </div>
                            </div>

                            <div class="container border rounded p-4">
                                <div class="form-floating">
                                    <label for="nextCity2">{{ __('Villes voisines 2') }}</label>
                                        <input type="text" class="form-control" id="nextCity2" placeholder="Si nouvelle ville" value="">
                                            <select class="form-select" id="nextCity2" aria-label="Floating label select example">
                                                <option selected></option>
                                                @foreach(App\Models\City:: get() as $ville)
                                                <option value="{{ $ville->id }}">{{ $ville->name }}</option>
                                                @endforeach
                                            </select>
                                </div>

                                <div class="form-floating pt-2">
                                    <label for="offerNextCity2">{{ __('Offres ville voisine 2') }}</label>
                                        <input type="text" class="form-control" id="offer1NextCity2" placeholder="" value="">
                                        <input type="text" class="form-control" id="offer2NextCity2" placeholder="" value="">
                                </div>

                                <div class="form-floating">
                                    <label for="demandNextCity2">{{ __('Demandes ville voisine 2') }}</label>
                                        <input type="text" class="form-control" id="demand1NextCity2" placeholder="" value="">
                                        <input type="text" class="form-control" id="demand2NextCity2" placeholder="" value="">
                                </div>

                            </div>

                            <div class="container border rounded p-4">
                                <div class="form-floating">
                                    <label for="story">{{ __('Histoire') }}</label>
                                            <textarea class="form-control" placeholder="Écrivez l'histoire ici" id="story" style="height: 100px">
Posté il y a 3 mois
Avatar de CinquièmeDimension
CinquièmeDimension

Salut,

Tas pas d'attribut name des ton form. Donc ton erreur n'est que la première d'une longue série.

<input type="text" class="form-control" id="name" placeholder="" value="" name="name">

Bon là c'est pas clair vue que tes données s'appellent aussi name... Donc deuxième exemple:

<input type="text" class="form-control" id="house" placeholder="" value="" name="house>

Il manquait ==> name="ton_nom"

Posté il y a 3 mois
Avatar de bestmomo
bestmomo

Salut,

Pour compléter la réponse :

 public function store(Request $request)
    {
            $city = new City;
            
            $city->name = $request->get('name');
            $city->year = $request->get('year');
            $city->house = $request->get('house');
            $city->yearfoundation = $request->get('yearfoundation');
            $city->size = $request->get('size');
            $city->weather = $request->get('weather');
            $city->accent1 = $request->get('accent1');
            $city->accent2 = $request->get('accent2');
            $city->localSpeciality = $request->get('localSpeciality');
            $city->mayor = $request->get('mayor');
            $city->urbanPopulaiton = $request->get('urbanPopulaiton');
            $city->ruralPopulaiton = $request->get('ruralPopulaiton');
            $city->tradeMax = $request->get('tradeMax');
            $city->localDrink = $request->get('localDrink');
            $city->education = $request->get('education');
            $city->fortification = $request->get('fortification');
            $city->lawAndOrder = $request->get('lawAndOrder');
            $city->technology = $request->get('technology');
            $city->wealth = $request->get('wealth');
            $city->economy1 = $request->get('economy1');
            $city->economy2 = $request->get('economy2');
            $city->economy3 = $request->get('economy3');
            $city->economy4 = $request->get('economy4');
            $city->economy5 = $request->get('economy5');
            $city->economy6 = $request->get('economy6');
            $city->economy7 = $request->get('economy7');
            $city->economy8 = $request->get('economy8');
            $city->economy9 = $request->get('economy9');
            $city->economy10 = $request->get('economy10');
            $city->offer1 = $request->get('offer1');
            $city->offer2 = $request->get('offer2');
            $city->nextCity1 = $request->get('nextCity1');
            $city->nextCity2 = $request->get('nextCity2');
            $city->offer1NextCity1 = $request->get('offer1NextCity1');
            $city->offer2NextCity1 = $request->get('offer2NextCity1');
            $city->offer1NextCity2 = $request->get('offer1NextCity2');
            $city->offer2NextCity2 = $request->get('offer2NextCity2');
            $city->demand1 = $request->get('demand1');
            $city->demand2 = $request->get('demand2');
            $city->demand1NextCity1 = $request->get('demand1NextCity1');
            $city->demand2NextCity1 = $request->get('demand2NextCity1');
            $city->demand1NextCity2 = $request->get('demand1NextCity2');
            $city->demand2NextCity2 = $request->get('demand2NextCity2');
            $city->story = $request->get('story');
            $city->version = $request->get('version');
            $city->chapter = $request->get('chapter');
            
            $city->save();
            
            return redirect()->route('profile.editor.index');
    }
Posté il y a 3 mois
Avatar de AlexandreCarignan
AlexandreCarignan

Un gros merci pour vos conseils, j'ai réussi. Je commence à bien comprendre le fonctionnement, avec la pratique j'apprends énormément. Alex.

Posté il y a 3 mois

Vous ne pouvez pas répondre à ce sujet.