Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Laravel and Databases Eloquent ORM

Model name doesn't match the table name

So, under "Basic Usage", in the eloquent documentation (http://laravel.com/docs/5.0/eloquent) I see that the Model class' name is used by eloquent to determine the table that's being invoked. However, in the video, we created a class whose name is "TodoList"; the table we created has a name="todo_lists". There is a mismatch between our class name and our table name.

Now, the documentation states that we can explicitly specify the table to be invoked by declaring a $table variable; the thing is, in the video Hampton Paulk doesn't explicitly specify the table name... The crazy part, is that this actually works!!

So, as may already be construed, my question is: why??? Why does it work? What did I miss?

If someone can clear this up for me, then I might be able to get some sleep tonight :-p

Thanks for the help!!

2 Answers

Alven Maretinus
Alven Maretinus
5,091 Points

Seems that Laravel has the capability to detect the singular form of the table name in the models' class name, by removing the given underscore characters if there are any. I tried this myself and it worked for me.

Thanks very much for this response... Over the past few weeks I've come to this very same conclusion. Unfortunately, I can't seem to find documentation to back it up; wish there was documentation for this so that I can tap into these "hidden capabilities"; I'll try to put asside some time to look at the actual framework and work out what it's doing.

Once again, thanks!

This is similar to CakePHP, I remember it's named Naming Convention.