PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, April 18, 2022

[FIXED] Why do table names have to be plural in Laravel?

 April 18, 2022     database, laravel     No comments   

Issue

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


Solution

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.



Answered By - crabbly
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing