Home  >  Q&A  >  body text

SQLSTATE: Column not found: 1054 Unknown column 'category_name' in 'field list' (SQL: insert into `categories`) to `categories`)

Illuminate database QueryException SQLSTATE[42S22]: 1054 Unknown column name 'category_name' in 'field list' (SQL: insert into categories (category_name, updated_at, created_at ) values ​​(fruits, 2022-08-08 10:46:52, 2022-08-08 10:46:52))

How to solve?

My controller name is "CategorieController". This is the store function sauvercategorie in my CategorieController.

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsCategorie;


class CategorieController extends Controller
{
    //

    public function ajoutercategorie(){
        return view('admin.ajoutercategorie');
    }

    public function sauvercategorie(Request $request){

        $validatedData = $request->validate([
            'category_name' => 'required | max:255',
        ]);

        $categorie = Categorie::create($validatedData);


        return redirect('/ajoutercategorie')->with('status', 'La catégorie'
        .$categorie->category_name.'a été ajoutée avec succès');

My entire blade file.

@extends('layouts.appadmin')

@section('title')
    Ajouter une catégorie
@endsection

@section('contenu')


    <div class="row grid-margin">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">Ajouter une catégorie</h4>

                  @if (Session::has('status'))
                    <div class="alert alert-success">
                        {{Session::get('status')}}
                    </div>
                    @endif
                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul>
                                @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form class="cmxform" id="commentForm" method="post" action="{{ route('categories.sauvercategorie') }}">
                      @csrf
                    <fieldset>

                      <div class="form-group">
                        <label for="cemail">Nom de la catégorie</label>
                        <input id="cemail" class="form-control" type="text" name="category_name" >
                      </div>

                      <input class="btn btn-primary" type="submit" value="Ajouter">

                    </fieldset>

                    </form>

                </div>
              </div>
            </div>
          </div>


@endsection

@section('scripts')
    {{--<script src="Administrateur/js/form-validation.js"></script>
    <script src="Administrateur/js/bt-maxLength.js"></script>--}}
@endsection

The name of the model is Category. This is my model

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Categorie extends Model
{
    use HasFactory;
        protected $fillable = ['category_name'];
}

My table name is "categories". This is my table

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('categorie_name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

Need help, thank you.

P粉933003350P粉933003350372 days ago598

reply all(1)I'll reply

  • P粉362071992

    P粉3620719922023-11-06 13:30:08

    In migration and model, your field names are different.

    Change your model to:

    protected $fillable = ['category_name'];

    changed to:

    protected $fillable = ['categorie_name'];

    reply
    0
  • Cancelreply