Laravel - Table does not exist

11,591

Solution 1

Because you don't follow Laravel naming conventions, you should define table manually:

protected $table = 'your_table_name';

Solution 2

For table with the name rolesStems your model must have the name RolesStem

So change the following line:

class RolesStems extends Model

to

class RolesStem extends Model

This is the default Laravel naming convention: Reference

If you don't want to follow this naming convention, then put the following code in your model:

protected $table = 'your_table_name';

So that your model refers to your_table_name

Solution 3

@user3242861, By default Laravel Try to find table according to Your Model Class name eg: your class "RolesStems" then it will try to find table "roles_stems" so if it is not available then it will give table or view not exists.

To override default functionality you can define table name explicitly in your model class as below.

protected $table = 'your_table_name';
Share:
11,591
user3242861
Author by

user3242861

Updated on June 22, 2022

Comments

  • user3242861
    user3242861 almost 2 years

    I create a table with php artisan make:migration command. After that i edit the migration file to put the columns and insert standard data. After i run php migrate:referesh command and all good, i go to the database and the table is there with a correct name and data.

    In database the table name is rolesStems.

    I create a model like this:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class RolesStems extends Model
    {
        protected $fillable = ['id', 'value', 'order', 'active','id_stem', 'id_role'];
    }
    

    And in my controller i do this:

    use App\RolesStems as RolesStem;   
    RolesStem::select('value')->where('active', '=', 1)->where('id_stem', '=', 1)->orderBy('id_role', 'asc')->get());
    

    And i have this error:

    SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.roles_stems' doesn't exist (SQL: select `value` from `roles_stems` where `active` = 1 and `id_stem` = 1 order by `id_role` asc)
    

    It put a unknown table name roles_stems, why?

    I do this to other table, the same way, but with this table this happens.

    What is the problem?

  • user3242861
    user3242861 about 7 years
    Works! Thank you
  • JessycaFrederick
    JessycaFrederick about 2 years
    Side note: I use the Jeffrey Way Pivot Table extender and it named the table something that caused this problem. github.com/laracasts/Laravel-5-Generators-Extended