Laravel:无法将数据插入数据库

时间:2014-12-23 21:20:18

标签: php laravel

我在使用雄辩模型插入将数据插入数据库时​​遇到问题。我试过了 Model->save()Model::create它并没有帮助我...我也在我的模型上定义了可填充的属性,但它没有用。

注意:我可以使用::all()::find从数据库检索数据,我也可以删除数据,但我无法将任何数据插入数据库。

我的型号代码是:

<?php
  class Game extends Eloquent{
  protected $table = 'games';
  protected $fillable = array('title','year');
}

我的控制器代码是:

<?php

class GameController extends BaseController{
public function doit(){
    $g= new Game;
    $g->title='Xina';
    $g->year='2014';
    //the error occur here
    $g->save();
    //also
    //Game::create(array('title='Xina','year'='2014'));
    //will give an error
    }
  }

任何人都可以帮助我吗?当我尝试我的代码时,我得到的唯一错误信息是:

  

糟糕,看起来出了问题。

1 个答案:

答案 0 :(得分:1)

根据你的错误:

Illuminate \ Database \ QueryException (42S22) SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into games (title, year, updated_at, created_at) values (Xina, 2014, 2014-12-24 00:03:27, 2014-12-24 00:03:27)) 

您没有在桌面上设置Laravel Timestamp列。

通过在模型上将timestamps属性设置为false来停止检查/设置这些列:

$timestamps = false;
相关问题