laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Why not put Models in App #1091

Open fengchenghao opened 6 years ago

fengchenghao commented 6 years ago

I have a question. Why not put models in App\Models directory by default. I always new a laravel project and mkdir Models folder in App and then move Users to App\Models.

OwenMelbz commented 6 years ago

It allows you to then structure it however you want.

If you google around there are lots of resources which explain why, for example this first one has reasoning http://laraveldaily.com/laravel-models-folder-not/

The laravel docs also explain why

When getting started with Laravel, many developers are confused by the lack of a models directory. However, the lack of such a directory is intentional. We find the word "models" ambiguous since it means many different things to many different people. Some developers refer to an application's "model" as the totality of all of its business logic, while others refer to "models" as classes that interact with a relational database.

For this reason, we choose to place Eloquent models in the app directory by default, and allow the developer to place them somewhere else if they choose.

More here -> https://laravel.com/docs/5.6/structure#introduction

mfn commented 6 years ago

I swear I've never heard anyone talking about models something else then the ORM relational stuff.

Funny how some things obvious so people (i.e. to me here) aren't to others 😄

I just checked my Models/ directory => 51. I'm glad early I already moved them into it and not having to see them every time I operate in app/

TL;DR: I always immediately create Models and move existing ones within:

To me it's a matter of organizing and not-so-technical what it means.

Personally, everything which "interacts" with Models and Services is in Repositories/ which doesn't exist per se either.

OwenMelbz commented 6 years ago

@mfn then you're lucky it can just be moved around :D

Tbh most of the time, I never even have the project tree open, I'm a keyboard shortcut kind of person, so barely ever actually navigate files via the tree, always using shortcuts and fuzzy searching to open the files, so doesn't really matter to me.

One thing that would be nice, is maybe a config item for "model directory" which then allows things like php artisan make:model Cake to automatically use the model directory, rather than having to do php artisan make:model Model\\Cake

martinbean commented 6 years ago

@fengchenghao Because not every one sticks their Eloquent models in a “Models” folder.

deleugpn commented 6 years ago

I recently faced this issue where a developer joined the team and was completely confused with the fact that the models folder was holding eloquent classes. I decided to rename models to eloquent and I was very happy with the namespace for it.

timacdonald commented 6 years ago

See this thread for more discussion around this also: https://github.com/laravel/laravel/pull/3821

hubertnnn commented 6 years ago

I have read this thread, and I understand the reasoning behind removal of Models directory. I am not suggesting moving models to App/Models (even though it would probably make most sense to most people) but to move them to a folder.

My reasoning is simple, even small projects have around 10 models, where big can reach hundreds. I was working on one project where models were stored in App and with 20 of them it was hard to scroll back and fourth the folder tree to open files. @OwenMelbz mentioned that he is using keyboard shortcuts to find files, but not everyone does, I for example prefer to do 4 mouse clicks to reach any file I want, But for that I need folders to not be too big.

Another issue with placing models in App is the fact that it violates "Rule of 30". If a project already have about 15 folders there for different submodules, than we have space for only around 15 models before we will consider it too big, while placing them in subfolder will double that amount.

As a summary I think Models default location should be moved to a folder (eg. App/Models) and that folder location should be configurable to allow artisan command work correctly regradless of its location.

OwenMelbz commented 6 years ago

@hubertnnn - discussion aside, what part of the artisan cli are you having trouble with? I thought all the make: commands have namespace support for structuring it as you like?

kevinobama commented 6 years ago

This is very interesting. I am working on an laravel project now. all of model files in Models folder apart from user.php model.I can move to Models folder. but we are busy to develop new features.

OwenMelbz commented 6 years ago

I do wonder what the possibility is of setting a config item to define the models namespace, allowing the artisan commend to generate them in the right place?

martinbean commented 6 years ago

Why do models, need to be in a models folder? You a free to define your own folder hierarchy.

Models represent the “guts” of your application. Applications can have many “parts”. For example, a project I’m currently working on has customer-related models in an App\Customers namespace; finance-related models in an App\Finance namespace; product- and stock-related models in an App\Inventory namespace; and so on. My models are organised based on their function and not solely the fact that they’re a model.

OwenMelbz commented 6 years ago

@martinbean they don't. Nobody is arguing that.

The argument is for many projects, a simple Models folder makes sense - so why is there not a more automated mechanism to allow this, maybe more focused at beginners, especially if they just move the User model into a models folder and wonder why their auth drivers are broken because they didn't know its mentioned in the configs etc.

So maybe its down to more of the documentation needs updating to explain users who want a models directory a recommended approach.

lucasmichot commented 6 years ago

https://github.com/laravel/laravel/pull/4680 is an alternative that introduces custom generator namespaces.

ghost commented 5 years ago

Mixing structure directories with organizational ones seems a bit odd (to say the least) to me. Laravel already makes assumptions about what a model is when it implements it with eloquent/artisan, so that excuse ends up being really weak.

MordiSacks commented 5 years ago

Checkout my personal fork https://github.com/MordiSacks/blaravel

connecteev commented 5 years ago

+1 I have a large Laravel project at this point (As I'm sure a lot of devs do), and there are 30+ models cluttering up my workspace. It really makes things hard to find and reduces my productivity.

I can't collapse the models into a folder like I can do for controllers, providers, etc. Please consider moving all models to App/Models, plenty of people (myself included) are suffering through this. See this workaround, which is rather clunky: https://medium.com/@codingcave/organizing-your-laravel-models-6b327db182f9

ridharezzag commented 4 years ago

Why laravel team are hardheaded and they dont wanna listen, everyone is complaining, i found it so confusing to just threw the modes outsider there, please listen to the complains

OwenMelbz commented 4 years ago

@ridharezzag remember, people will come to GitHub to complain, but those people who are happy will not. So maybe 100 people will complain on github, but 2000 will be happy outside

crynobone commented 4 years ago

everyone is complaining

Not everyone, I tend to ignore this issue because this is your channel to express what you want on the framework. I for instance use both structure and have no problem with the current setup.


But if you indeed want to customise the the make:model (or any generator related to Laravel) here's an alternative (for Laravel 6).

composer require --dev "orchestra/canvas=^4.5"

Then either use ./vendor/bin/canvas preset laravel or manually create a canvas.yaml file at the root directory of your Laravel project and edit the content to:

preset: laravel

namespace: App
user-auth-provider: App\Models\User

model:
  namespace: App\Models

Whenever you use artisan with make:model, make:observer, make:factory or make:policy the App\Models namespace and App\Models\User will be honoured.

GitHub: https://github.com/orchestral/canvas

martinbean commented 4 years ago

@ridharezzag Not everyone stores their models in a models directory!

What’s confusing about being able to place a model where you want? If you want to put models in a models directory, then do so.

hubertnnn commented 4 years ago

There is already another issue where we suggested a rework of make:xxx including make:model: https://github.com/laravel/ideas/issues/1854#issuecomment-536826871

With orchestral\canvas around I think there is a good chance that make:xxx commands will be removed from core in the near future, so there is probably no need to debate about location of models any more.

eshe-huli commented 4 years ago

so @OwenMelbz & @martinbean the change will be made if many come to this thread and add they complains? I was not complain because I was thinking that it had a better reason of why this is not default to put "model" in a "Models" folder đŸ€Ż?