简体   繁体   English

Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_name' in 'field list' (SQL: insert into `categorie

[英]Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_name' in 'field list' (SQL: insert into `categorie

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)) 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 ) 值 (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.我的 controller 的名称是“CategorieController”。这是我的商店 function 在 CategorieController 中称为 sauvercategorie。

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 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 model is Categorie. model 的名称是类别。 This is my model这是我的 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.需要帮助解决这个问题,谢谢。

your field name in migration and model is different.您在迁移中的字段名称和 model 不同。

Change your model from:将您的 model 从:

protected $fillable = ['category_name'];

into:进入:

protected $fillable = ['categorie_name'];

This is occuring error because you have different column name in your Categorie model.发生此错误是因为您的类别 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Illuminate\Database\QueryException SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“current_team_id” - Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'current_team_id' in 'field list' 消息为&#39;SQLSTATE [42S22]的Illuminate / Database / QueryException:找不到列:1054未知列 - Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 如何修复“Illuminate / Database / QueryException with message&#39;SQLSTATE [42S22]:找不到列:1054未知列”? - How to fix “Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column”? SQLSTATE[42S22]:未找到列:1054 &#39;field list&#39; 中的未知列 &#39;_token&#39;(SQL:插入到“产品”中 - SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list' (SQL: insert into `products` Laravel SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ 0”(SQL:插入到“ add_file”(“ 0”)中) - Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `add_file` (`0`) Laravel SQLSTATE [42S22]:未找到列:1054“字段列表”中的未知列“testimonial_by”(SQL:插入“testimonials” - Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column 'testimonial_by' in 'field list' (SQL: insert into `testimonials` SQLSTATE [42S22]:未找到列:1054“字段列表”中的未知列“提供者”(SQL:插入“oauth_clients” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'provider' in 'field list' (SQL: insert into `oauth_clients` QueryException SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ customers.id” - QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.id' in 'where clause' SQLSTATE [42S22]:未找到列:1054&#39;字段列表Laravel中的未知列&#39;0&#39; - SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list Laravel SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列&#39;$ Valuess&#39; - SQLSTATE[42S22]: Column not found: 1054 Unknown column '$Valuess' in 'field list''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM