You can have models anywhere you want them. The built in User
model is placed directory under app/
. If you want to use a specific folder for all your models, create on inside app/
named Models
and then namespace all your models with App\Models\X
.
Yes, but as I mentioned in my previous post, I'm wondering if there are any particular conventions at this point. Are models just in the App\
namespace because they belong there, or are they there because they are supposed to be there once Laravel 5 is officially released?
Following framework conventions makes contributions easier, and when new team members are brought on board, they know where everything is. So if Laravel 5 is going to say that models are just scattered in the App\
namespace just as they currently are, I should either do that,or document the fact that my models are in App\Models
again and again so that team members would know what's going on.
That's why I asked: is this a convention, or is this going to change?
There are many conventions for "Models" - Laravel just doesn't bottleneck you or your project into using any one method. This is something for you (and your development team, if you're a part of one) to decide on. That is the convention behind Laravel - giving you the developer choice in the matter.
If you're just starting out in Laravel and are simply learning, leaving your models within the App directory is just fine - there's nothing wrong with it.
I'd give this article by Dayle Rees a read on the matter as well: http://daylerees.com/breaking-the-mold
Thanks for the response, @kaidesu. Now that I know it is a do-it-however-you-wish matter, I created a folder inside the App\
namespace. Having models scattered in a directory with other directories in there gives me a bad feeling, just like a PHP file that has no classes and is a mishmash of statements.
Glad I could help! Creating a directory is a perfect acceptable way to group together your models.
I would also look into the Repository Pattern - which is more suited to larger scale applications. I'm not saying you should use it for your current project, but as simply something to learn about to add to your developer tool belt.
Here's some resources on the matter:
Edit
I also forgot about this Laracasts video which would be helpful in general as well:
They normally sit in the app
directory now as of Laravel 5.
my guess is they were left in the App
namespace so you had less to type when you referenced your Models. Instead of App\Models\User::find(1)
you only have to type App\User::find(1)
. I'm still debating if the extra typing is worth the extra organization.
Other things are grouped in folders a little more generously because for the most part Laravel automatically tacks on the namespace for you.
Im with Parham, it seems really disorganized to not have a specific place for models. Not sure its an imporovement
bradtraversy said:
Im with Parham, it seems really disorganized to not have a specific place for models. Not sure its an improvement
Agreed. I'm going through the upgrade from 4.2 as we speak and this didn't make sense to me. I'll accept it for what it is and move on. Solutions to build ;)
I place all my data classes together:
app/
data/
meta/
models/
repos/
settings/
Works for me.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community