EUITSols Mahfuj EUITSols Mahfuj - 4 months ago 12x
PHP Question

How can I change Laravel SQL Table Query?

My Laravel User Table name is s_user but result is:

SQLSTATE[42S02]: Base table or view not found: 1146 Table
'laravel_learn.users' doesn't exist (SQL: select * from
= 1 limit 1) (View:

Please help me! how can i change users table. my table name is s_user but laravel query result show users but why? how can i change it users to s_user?


Since you have not defined that what the table name is, Eloquent automatically, searches for snake cased plural form of your model's name, in this case users, furthermore, as it can not locate it, you get that error.

So to edit it, within the app\User.php file, which is your User model, set the $table property.

For your other question, which you have posted as an answer as how to change the primary key field, Eloquent assumes you have an primary column named as id, to change it, set it within the $primaryKey variable.

class User extends Model
    // Define table name explicitly
    protected $table = 's_user';

    // Define primary key explicitly
    protected $primaryKey = 'user_id';

All of those information are written in the Laravel documentation, do not forget it to check it out.