【问题标题】:Laravel inserting dataLaravel 插入数据
【发布时间】:2014-12-12 13:21:34
【问题描述】:

我有 FotoController.php

这是功能的一部分:

$img = array(
    'name' => $filename,
    'gid' => $id,
);

$s = new Img($img);         
$create = $s->save();

我的 img 模型:

class Img extends Eloquent {

 public $timestamps = false;

 /**
 * The database table used by the model.
 *
 * @var string
 */
 protected $table = 'images';

 /**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
 //protected $hidden = array('remember_token');

 protected $fillable = array('name', 'gid', 'views', 'likes');


}

但它不会将新记录添加到数据库中。

【问题讨论】:

  • 你可以像这样使用create方法; Img::create($img);
  • 你能给我举个例子吗?
  • 阅读评论,您会找到示例...

标签: database laravel insert


【解决方案1】:

这种方式不能创建新对象同时填充,需要使用fill方法:

$img = array(
    'name' => $filename,
    'gid' => $id,
);

$s = new Img();
$s->fill($img);      
$s->save();

或者您可以在模型上使用静态创建方法:

$img = array(
    'name' => $filename,
    'gid' => $id,
);

$s = Img::create($img);

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 2020-07-05
    • 2021-05-11
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多