[英]Laravel : Can not insert data into database
我在使用雄辯的模型插入將數據插入數據庫時遇到問題。 我嘗試了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
}
}
有人可以幫助我嗎? 當我嘗試我的代碼時,我得到的唯一錯誤消息是:
哎呀,看起來像出事了。
根據您的錯誤:
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時間戳列。
通過在模型上將timestamps屬性設置為false,停止檢查/設置這些列:
$timestamps = false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.