Base table or view not found: 1146 Table Laravel 5

PhpMysqlLaravel

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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";

Solution 2 - Php

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)  {
    // ...
});

Solution 3 - Php

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)

Solution 4 - Php

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.

Solution 5 - Php

Just run the command:

php artisan migrate:refresh --seed

Solution 6 - Php

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.

Solution 7 - Php

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',            ];
        }

Solution 8 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionray sn0wView Question on Stackoverflow
Solution 1 - PhpJames SpenceView Answer on Stackoverflow
Solution 2 - PhpInamur RahmanView Answer on Stackoverflow
Solution 3 - PhpsaoView Answer on Stackoverflow
Solution 4 - PhppanjehView Answer on Stackoverflow
Solution 5 - PhpAbhiView Answer on Stackoverflow
Solution 6 - Phpsamad.naghipourView Answer on Stackoverflow
Solution 7 - PhpHadiView Answer on Stackoverflow
Solution 8 - PhpMD SHAYONView Answer on Stackoverflow