2015-06-02 62 views
1

嘗試向Laravel中插入新獲取的數據時,我無法將2個字段(即類型和提供程序)插入到數據庫中。無法使用laravel在數據庫中插入所有內容

這裏是我的代碼:

$user = User::create([ 
    'name' => $userData->name, 
    'username' => $userData->name, 
    'email' => $userData->email, 
    'password' => 'asdffdas', 
    'type' => 'standard', 
    'provider' => $userData->id, 
    'active' => 1 
    ]); 

如果我贊同$userData->id,我設法得到一個有效的整數,但是在插入後,該類型始終是第一enum而供應商始終爲0

任何想法可能會發生這種情況?

+0

可能是你想echo'$ user-> id',而不是'$ userData-> id'? –

+0

偶然提供者是'tinyint'嗎? – Augwa

回答

3

您必須確保字段位於$ fillable = [];你的模型的參數。

質量分配

當創建一個新的模式,你將屬性的模型構造的數組。這些屬性隨後通過質量分配分配給模型。這很方便;然而,盲目地將用戶輸入傳遞到模型中可能是一個嚴重的安全問題。如果用戶輸入被盲目傳遞到模型中,用戶可以自由修改模型的任何和所有屬性。出於這個原因,所有Eloquent模型默認情況下都不能進行批量分配。

To get started, set the fillable or guarded properties on your model. 

Defining Fillable Attributes On A Model 

The fillable property specifies which attributes should be mass-assignable. This can be set at the class or instance level. 

class User extends Eloquent { 

    protected $fillable = array('first_name', 'last_name', 'email'); 

} 
相關問題