2014-12-04 83 views
0

我試圖使用Laravel的create方法Laravel創建方法

$input = Input::all(); 
$new_media = $this->media->create($input); 

$input = Input::all(); 
$new_media = Media::create($input); 

兩個代碼不起作用存儲陣列。

這是我得到的錯誤。

Method [create] does not exist. 

Laravel文檔這裏http://laravel.com/docs/4.2/eloquent

從laravel文檔

// Create a new user in the database... 
$user = User::create(array('name' => 'John')); 

介質模型

<?php 

class Media extends Eloquent { 

    protected $table = 'media'; 

    protected $guarded = array(); 

    public static $rules_video = array(
     'title' => 'required', 
     'url' => 'required' 
    ); 
    public static $rules_image = array(
     'title' => 'required' 
    ); 

    public function category(){ 
     return Category::find($this->category_id); 
    } 

} 
+0

媒體模型是否擴展了雄辯? – Richie 2014-12-04 11:45:44

+0

我猜想沒有這樣的方法,而是可以使用像這樣的$ user = new User; $ user-> name ='John'; $ user-> save(); 其中用戶是你的模型類擴展雄辯 – 2014-12-04 11:46:02

+0

@裏奇是的,它的確是! – 2014-12-04 11:47:13

回答

2

您使用createfillupdate時需要設置Mass Assignments。見this answer

刪除您的guarded屬性,並創建一個新名爲fillable的數組,允許使用質量分配更新允許的字段。

protected $fillable = ['title', 'url'];