18

What is the correctly table name of this table in Laravel 3/4?

Structure
image_projects (id, project_id, image, ext, size, created_at, updated_at, active)

image_projects
imageprojects
imageProjects

And, how I can create the Model?

app/models/image_projects.php
app/models/imageprojects.php
app/models/imageProjects.php
app/models/image/projects.php
app/models/projects/image.php

2 Answers 2

53

It makes no difference what you name your table, as long as you then name the file & class in singular form, with the class name starting with an uppercase letter.

You can use any of the following options:

Table name: image_projects

File name: ImageProject.php

Class name: ImageProject


Table name: imageprojects

File name: Imageproject.php

Class name: Imageproject


Table name: imageProjects

File name: ImageProject.php

Class name: ImageProject

In this case you'll have to set the $table property yourself.


Remember: If you don't name your class in singular form of what you name your table, you'll have to manually set it in your model:

class ImageProjects extends Eloquent
{
    public $table = 'image_projects';
}
9
  • 1
    Also, remember that if you use underscores in your class name, you'll need to nest your models into folders (Image_project would live as /Image/project.php)
    – mavrck
    Jan 22, 2013 at 1:28
  • @mavrck - I don't think that you have to. It's an option. Jan 22, 2013 at 1:29
  • @joe If you're using version 3 or 4 I believe it's required, unless you want to hack the start.php file. Because of PSR-0 rules, underscores are converted. (See here: github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)
    – mavrck
    Jan 22, 2013 at 1:34
  • @JosephSilber só, the 'best pratice' is use underscore and 'nest models' into folders? Jan 22, 2013 at 1:38
  • @PatrickMaciel - Yes. It seems I was wrong in assuming that folders are optional. Jan 22, 2013 at 1:41
1

Current Laravel version 4.2 table name convention works OK in that way:

Table name: image_projects

File name: ImageProject.php

Class name: ImageProject

The camel-case class name forced me with an exception to use table name underscores.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.