Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column '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 that ??
The name of my controller is "CategorieController".This is my store function called sauvercategorie in CategorieController.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Categorie;
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 >
<div >
<div >
<div >
<h4 >Ajouter une catégorie</h4>
@if (Session::has('status'))
<div >
{{Session::get('status')}}
</div>
@endif
@if ($errors->any())
<div >
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form id="commentForm" method="post" action="{{ route('categories.sauvercategorie') }}">
@csrf
<fieldset>
<div >
<label for="cemail">Nom de la catégorie</label>
<input id="cemail" type="text" name="category_name" >
</div>
<input 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 model is Categorie. This is my model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Categorie extends Model
{
use HasFactory;
protected $fillable = ['category_name'];
}
my table name is "categories". This is my table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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 helps to solve that, thanks.
CodePudding user response:
your field name in migration and model is different.
Change your model from:
protected $fillable = ['category_name'];
into :
protected $fillable = ['categorie_name'];
CodePudding user response:
This is occuring error because you have different column name in your Categorie model.
Change this,
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('categorie_name');
$table->timestamps();
});
to this,
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category_name');
$table->timestamps();
});
and run the command
php artisan migrate:refresh