Error 500 with save() method of eloquent/laravel

- 1 answer

Ad

I want to save an object with Laravel's eloquent but I get error 500 when trying to save.

This is my model code:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categorie extends Model
{

}

and this is my action:

public function addCategorie() {
    $input = Input::all();
    $categorie = new Categorie;
    $categorie->libelle = $input['label'];
    $categorie->save();
    return $categorie;
}

When I use print_r($categorie) after the save method I get a result, but when I use the save() method I got this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into `categories` (`libelle`, `description`, `updated_at`, `created_at`) values (sss, zzz, 2015-12-23 16:20:30, 2015-12-23 16:20:30 ))

Ad

Answer

Ad

In your migration file for categories table, you need to have

Schema::create("categories", function($table){
  // Id, name, etc
  $table->timestamps();
}

so that eloquent queries can update when this entry was created and saved, or created_at and updated_at. If you don't want to use these timestamps, add

class Categorie extends Model {
  public $timestamps = false;
}

to your Categorie model.

Ad
source: stackoverflow.com
Ad