Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel assumes the database table is the plural form of the model name

Tags:

By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is "news" and i still want to use this feature? should i change it to "newses" or should i use "new" for the model name?

like image 816
Mervyn Yet Avatar asked Jun 07 '16 08:06

Mervyn Yet


People also ask

Should table name be plural or singular laravel?

In laravel the table names are plural by default and "id" is the default primary key. The pivot table is "derived from the alphabetical order of the related model names". So in your example it would be role_user, not user_role. Last updated 4 months ago.

Are database tables plural or singular?

So Which Should I Use: Singular or Plural? The correct answer is that there is no right or wrong when naming database tables — just be consistent from the start. The only wrong answer with database table names is to use a combination of plural and singular.

What is model name in laravel?

Laravel Create Model is an MVC based PHP system. In the MVC architecture, 'M' stands for 'Model'. A model is used as a way for questioning data to and from the table within the database. Laravel gives a basic way to do that using Eloquent ORM where each table incorporates a Model to interact with it.

Is database plural or singular?

The plural form of database is databases.


1 Answers

You may specify a custom table by defining a table property on your model as below

<?php  namespace App;  use Illuminate\Database\Eloquent\Model;  class Flight extends Model { /**  * The table associated with the model.  *  * @var string  */ protected $table = 'my_flights'; } 

Ref:https://laravel.com/docs/5.1/eloquent

like image 82
Vijayanand Premnath Avatar answered Sep 28 '22 17:09

Vijayanand Premnath