56

Im getting this error when I try to save data to mysql using Laravel 5, other forms and save() methods work fine but this one:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sistemal5.cotizacions' doesn't exist (SQL: insert into `cotizacions` (`customer_id`, `total`, `updated_at`, `created_at`) values (1501, 150.69, 2015-05-11 03:16:25, 2015-05-11 03:16:25))

Here is my Controller store method:

public function store(CotFormRequest $request)
    {    
        $quote = new Cotizacion;
        $quote->customer_id = Input::get('data.clientid');
        $quote->total = Input::get('data.totalAftertax');    
        $quote->save();    
    }

And here is my model:

<?php namespace App\Models\Cotizacion;

use Illuminate\Database\Eloquent\Model;


class Cotizacion extends Model {

}

I must be overlooking something really obvious cause i cant understand why Laravel is adding an "S" the table is cotizacion not cotizacions.

How can i troubleshoot this?

ray sn0w
  • 1,159
  • 3
  • 11
  • 24

8 Answers8

119

I'm guessing Laravel can't determine the plural form of the word you used for your table name.

Just specify your table in the model as such:

class Cotizacion extends Model{
    public $table = "cotizacion";
James Spence
  • 2,020
  • 1
  • 16
  • 23
  • 7
    The problem is that laravel is ADDING the 'S', why? – ray sn0w May 11 '15 at 03:38
  • 12
    That is the standard for Laravel. They, by default, pluralize table names (i.e. the model `Business` is for the table `businesses`). If you don't like, you need to specify your table name manually. – James Spence May 11 '15 at 03:40
  • 2
    If you're looking for clarification it's explained in [Eloquent docs here](http://laravel.com/docs/5.0/eloquent#basic-usage) – James Spence May 11 '15 at 03:45
  • The most weired thing for my, that I have two tables, `eqtypes` and `equipments`, guss who's laravel could not able to determine its plural?! It is `equipments` – SaidbakR Mar 18 '17 at 09:11
  • 4
    That's because the plural of equipment is equipment – James Spence Mar 18 '17 at 14:46
  • Sometimes we may be provided with the database already, rather than making it through migrations and here this answer is very helpful – DragonFire Nov 04 '19 at 08:28
15

Check your migration file, maybe you are using Schema::table, like this:

Schema::table('table_name', function ($table)  {
    // ...
});

If you want to create a new table you must use Schema::create:

Schema::create('table_name', function ($table)  {
    // ...
});
Inamur Rahman
  • 2,913
  • 1
  • 27
  • 29
7

I faced this problem too in laravel 5.2 and if declaring the table name doesn't work,it is probably because you have some wrong declaration or mistake in validation code in Request (If you are using one)

sao
  • 83
  • 1
  • 7
3

If your error is not related to the issue of

Laravel can't determine the plural form of the word you used for your table name.

with this solution

and still have this error, try my approach. you should find the problem in the default "AppServiceProvider.php" or other ServiceProviders defined for that application specifically or even in Kernel.php in App\Console

This error happened for me and I solved it temporary and still couldn't figure out the exact origin and description.

In my case the main problem for causing my table unable to migrate, is that I have running code/query on my "PermissionsServiceProvider.php" in the boot() method.

In the same way, maybe, you defined something in boot() method of AppServiceProvider.php or in the Kernel.php

So first check your Serviceproviders and disable code for a while, and run php artisan migrate and then undo changes in your code.

panjeh
  • 1,310
  • 1
  • 14
  • 11
2

Just run the command:

php artisan migrate:refresh --seed
Pingolin
  • 3,161
  • 6
  • 25
  • 40
Abhi
  • 119
  • 1
  • 2
0

I also had this problem when doing migration => after performing php artisan migrate:refresh --seed command

"Base table or view not found: 1146 Table posts do not exist" and I solved it, I was using "soft deletes" in a separate migration file, and my original migration for "posts" table which was responsible for creating the "posts" table was performing after the "soft deletes" migration in the database\migration folder, I simply needed to rename the "posts" migration file name and put it before "softdeletes" migration to perform sooner. the do the migration again => php artisan migrate:refresh --seed it's done.

0

When you are passing the custom request in the controller method, and your request file doesn't follow the proper syntax or table name is changed, then laravel through this type of exception. I'll show you in Example.

My Request code.
     public function rules()
        {
            return [
                'name' => 'required|unique:posts|max:50',
                'description' => 'required',
            ];
        }

But my table name is todos not posts so that's why it through "Base table or view not found: 1146 Table Laravel 7


    I forgot to change the table name in first index.
     public function rules()
        {
            return [
                'name' => 'required|unique:todos|max:50',
                'description' => 'required',
            ];
        }
Hadi
  • 38
  • 1
  • 9
0

This problem occur due to wrong spell or undefined database name. Make sure your database name, table name and all column name is same as from phpmyadmin.

Thank You

MD SHAYON
  • 7,001
  • 45
  • 38