skip to Main Content

I’m trying to nake a simple to save a simple Product in my db but for a reason I don’t understand, I get this error each time I try to save.

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘files’ in ‘field list’

The thing is that I don’t have any Files column and I don’t want to have one.

Here’s my migration file :

 public function up()
{
    Schema::create('formations', function (Blueprint $table) {
        $table->id();
        $table->string('name')->unique();
        $table->string('subtitle');
        $table->text('description');
        $table->integer('price');
        $table->integer('category_id');
        $table->integer('level_id');
        $table->timestamps();
    });
}

Here’s my comtroller file :

public function store(Request $request)
{
    $request->validate(([
        'name' => 'required|string',
        'price' => 'required',
        'description' => 'required',
        'subtitle' => 'required',
        'category_id' => 'required',
        'level_id' => 'required',
    ]));

    Formation::create($request->all());

    return redirect()->route('admin.formations.index')->with('success','Formation ajoutée');
}

I’m confused!

Edit :

Here’s the dd of $request

   array:8 [▼
  "_token" => "AGHU3QcTxNEd29ZVJ2mNM1lGMwAHDMqbIQEG4XxU"
  "name" => "Lh  lU"
  "price" => "125"
  "category_id" => "1"
  "level_id" => "2"
  "subtitle" => "Inke hune us kogru wuwuvat kerudowe anuzti gosvili dutoc wiv dufeaba job. Vaamcoj zodli kecuh wu ri hari sisalal gajesma ate ihloef egkes li zu. Ezfaaw uzared c ▶"
  "description" => "<p>gg<br></p>"
  "files" => null
]

It says "files" => null, but I don’t know where that Files field comes from.

Nothing in my migrations and nothing in the db (PhpMyAdmin).

Really strange!

2

Answers


  1. There is a files in your request, so you get this error. You can save your query in this way, to prevent extra field :

    $q = new Formation();
    $q->name = $request->name;
    $q->price = $request->price;
    $q->description = $request->description;
    $q->subtitle = $request->subtitle;
    $q->category_id = $request->category_id;
    $q->level_id = $request->level_id;
    $q->save();
    
    Login or Signup to reply.
  2. Use $request->validated() instead of $request->all()

    public function store(Request $request)
    {
        // ...
        Formation::create($request->validated());
        // ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search