Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Leff7's avatar
Level 4

Laravel - after renaming tables - call to undefined method updateOrCreate()

After having to rename database tables and running migrations I got:

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

For this piece of code:

  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
    ]);
  }

Then I have tried with hardcoding new table names like so:

     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
        ]);
      }

But then I got the new error:

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

How can I fix that?

0 likes
2 replies
stanb's avatar
stanb
Best Answer
Level 3

updateOrCreate does not work on de DB class directly, this is an Eloquent method.

Laravel expects a certain table name for a model. For example if you create a model 'user', Laravel expect that the table name is 'users'. When you do not want this standard way of Laravel you have to give the desired table name in your model with the protected variable $table.

Class ModelName extends Model
{

    protected $table = 'name_of_your_table';

}
Screenbeetle's avatar

Hi Drax7

Re. Your first error: Somethings to consider

  • Did you migration actually work (is the table there)?
  • Have you updated your models/namespaces/use statements?
  • Is your table called shop or shops? By default I think Laravel expects plural for table names, singular for models. But you can override this in the model.

Re. Your second error I believe for your second error - updateOrCreate is an eloquent method so wont work with the query builder (DB::table('calc_shop') is using the query builder not eloquent).

Please or to participate in this conversation.