2014-04-19 38 views
1

內工作,我一直在嘗試失敗的包(我第一次),我試圖發展內更新數據庫記錄。這是一個文件上傳腳本,它假設將文件上傳到一個目錄,保存它的路徑,用戶給它的名字和分配給數據庫的一組標籤。插入完美的作品。但是,當文件更新爲新標籤(同名)時,它只是給了我一個空白的迴應。我的實現如下:Laravel雄辯選擇(firstornew/firstorcreate/firstorupdate)似乎不包

$filename = Input::get('filename') . '.' . Input::file('file')->getClientOriginalExtension(); 
$tags = Input::get('tags'); 

$doc = Document::firstOrNew(array('filename' => $filename)); 
$doc->tags = $tags; 
$saved = $doc->save(); 

文檔模型如下:

class Document extends Eloquent { 

    protected $table = 'documents'; 
    protected $softDelete = true; 
    protected $fillable = array('filename'); 

    public function __construct($attributes = array()) { 
     parent::__construct($attributes); 
    } 

} 

我在做什麼錯?爲什麼當插入完美時「選擇」不工作?

回答

0

老問題,但我想我會回答只是要整齊。看起來你缺少模型中的關係定義:

public function tags() 
{ 
    return $this->hasMany('App\YourTagsModel'); 
} 

而且反過來,你的標籤模型應該有相反的定義:

public function document() 
{ 
    return $this->belongsTo('App\YourDocumentModel'); 
}