JianNCI
JianNCI

Reputation: 179

laravel set model for an exist database

I am learning laravel and I have a local database and wanna performing CRUD operation with it through laravel, I am wondering how can I create a model for that database tables?

I am attempting to create a model on the fly via artisan:

php artisan make:model local_database_user 

but this user model won't represent for the user table in my database even I made all the configuration in .env file.

I am wondering how my local_database model can really represent my database so I can use the eloquent methods with it?

Upvotes: 1

Views: 427

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

After setting up the connection in your .env file, you could set the name of the associated table in your Model using $table variable, like :

class ModelName extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'username'; //table_name
}

NOTE: Like @MartinHenriksen says

it easier to just follow model naming conventions so table property is automatically generated, so if your table is named user_admins call your model UserAdmin and you won't have to explicitly define it

Upvotes: 4

Related Questions