【问题标题】:Laravel inserting new recordLaravel插入新记录
【发布时间】:2015-02-02 23:35:22
【问题描述】:

我在向数据中插入新记录时遇到问题

有一个videocontroller的部分:

  $galleryName = Input::get('name');
  $galleryID = Foto::create($galleryName);

这里的型号:

class Foto extends Eloquent {

    public static function create($name)
    {
        return DB::table('galleries')->insert_get_id(array('name' => $name));
    }
}

Foto::create()的声明应该兼容

Illuminate\Database\Eloquent\Model::create(array $attributes)

我应该解决什么问题?

【问题讨论】:

  • 像@JosephSilber 一样,或者使用返回单个元素数组的Input::only('name')

标签: database laravel model insert


【解决方案1】:

Eloquent 的 create 方法接受一个数组。你无法改变它。

如果您需要此功能,请创建不同的方法:

public static function createFromName($name)
{
    return DB::table('galleries')->insertGetId(compact('name'));
}

然后在你的控制器中使用它:

$galleryName = Input::get('name');

$galleryID = Foto::createFromName($galleryName);

【讨论】:

  • 当我想将数组添加到数据库时:$tab = array('name' => Input::get('name'), 'user_id' => Auth::id()); Foto::create($tab); 模型应该如何看?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 2015-05-10
  • 1970-01-01
  • 2013-02-06
相关资源
最近更新 更多