如何将数组插入数据库 - laravel

时间:2014-12-05 11:33:02

标签: laravel model insert

当我想将数组添加到数据库时:

$tab = array(
   'name' => Input::get('name'), 
   'user_id' => Auth::id()
); 

Foto::create($tab);

MODEL:

<?php 
   class Foto extends Eloquent { 
       public static function create($name) { 
            returnDB::table('galleries')->insert(array('name' => $name['name'], 'user_id' => $name['user_id'])); 
        } 
    }

模型应该怎么样?

1 个答案:

答案 0 :(得分:0)

在控制器(FotoController.php)中,您可以编写如下内容:

class FotoController extends BaseController {

   public function create() {
        $foto= new Foto(
            array(
             /*Field's form*/
             "name" => Input::get("name"), 
             "user_id" => Input::get("user_id"),
            )
        );
        $foto->save();
    }
}