2017-05-03 36 views
0

具有重命名數據庫表和運行的遷移我後:Laravel - 重命名錶之後 - 調用未定義的方法updateOrCreate()

SQLSTATE[42S02]: Base table or view not found

對於此片的代碼:

Shop::updateOrCreate(
    ['id' => $id], 
    ['title' => $shopeTitle] 
); 

    Mall::where('id', $id)->update([ 
    'price' => $price, 
    'visitors' => $visitors 
    ]); 

    if ($mall != 41 || $mall!= 42) { 
    DB::table('calc_stores_in_malls')->insert([ 
     'mall' => $price, 
     'store' => $id 
    ]); 
    } 

然後我有硬編碼的新表名試圖像這樣:

 DB::table('calc_shop')->updateOrCreate(
     ['id' => $id], 
     ['title' => $shopeTitle] 
    ); 

     DB::table('calc_stores_in_malls')->where('id', $id)->update([ 
     'price' => $price, 
     'visitors' => $visitors 
     ]); 

     if ($mall != 41 || $mall!= 42) { 
     DB::table('calc_stores_in_malls')->insert([ 
      'mall' => $price, 
      'store' => $id 
     ]); 
     } 

但後來我得到了新的錯誤:

Call to undefined method Illuminate\Database\Query\Builder::updateOrCreate()

我該如何解決這個問題?

+2

'updateOrCreate'是雄辯的功能,它使用的模型。你用'DB :: table'繞過了這個。爲什麼不更新模型中的表名? – aynber

+1

順便說一句:你知道'($ mall!= 41 || $ mall!= 42)'總是TRUE嗎? –

回答

1

updateOrCreate是一個Eloquent函數。

最簡單的方法是通過你的模型,並通過執行更新表名稱如下:

$protected $table = 'new_table_name'; 
相關問題