user3242861
user3242861

Reputation: 1929

Laravel - Table does not exist

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?

Upvotes: 3

Views: 16099

Answers (3)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26288

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

Upvotes: 5

user4094161
user4094161

Reputation:

@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';

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

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

protected $table = 'your_table_name';

Upvotes: 11

Related Questions