Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do table names have to be plural in Laravel?

I noticed that whenever I create a class using database information in Laravel, the table name must be plural. Why is this?

like image 832
Simon Suh Avatar asked May 31 '16 21:05

Simon Suh


1 Answers

They don't have to be plural. To make things easy, by default, Eloquent assumes that, if you have a model User, your database table for it will be users. However, if you want to have another name for your table, you can specify custom tables for your models, by defining a table property. For ex:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'user';

    ...
}

This will tell Eloquent to use the table user on your database when working with the model User.

like image 183
crabbly Avatar answered Oct 03 '22 22:10

crabbly