【问题标题】:Error 500 with save() method of eloquent/laraveleloquent/laravel 的 save() 方法出现错误 500
【发布时间】:2016-03-30 03:02:04
【问题描述】:

我想用 Laravel 的 eloquent 保存一个对象,但尝试保存时出现错误 500。

这是我的型号代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categorie extends Model
{

}

这是我的行动:

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

当我在保存方法之后使用print_r($categorie) 时,我得到了一个结果,但是当我使用save() 方法时,我得到了这个错误:

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列 'updated_at'(SQL:插入到 `categories`(`libelle`、`description`、`updated_at`、`created_at`)值( sss, zzz, 2015-12-23 16:20:30, 2015-12-23 16:20:30))

【问题讨论】:

  • 是的 .. 未找到列 ..
  • 此错误表示您的数据库中缺少此列。当你进行迁移时,你应该包括 $table->timestamps();到您的迁移以将这两个字段添加到您的表中

标签: php laravel laravel-5 eloquent laravel-5.1


【解决方案1】:

categories 表的迁移文件中,您需要有

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

以便eloquent 查询可以在创建和保存此条目时更新,或者created_atupdated_at。如果您不想使用这些时间戳,请添加

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

到您的Categorie 模型。

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2018-04-05
    • 2016-05-27
    • 2020-10-02
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    相关资源
    最近更新 更多