-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] PrimaryKey attribute for Eloquent Models #51290
[11.x] PrimaryKey attribute for Eloquent Models #51290
Conversation
I am a huge fan of this! I think it will be easier to remember and auto-completable than the current approach. |
src/Illuminate/Database/Eloquent/Concerns/HasCustomizations.php
Outdated
Show resolved
Hide resolved
src/Illuminate/Database/Eloquent/Concerns/HasCustomizations.php
Outdated
Show resolved
Hide resolved
This is one of those PRs that potential opens the door to many more PRs. For example, defining the table for the model... the database connection... thoughts? |
@taylorotwell A DB or Database attribute to set the table, connection and timestamps. |
@taylorotwell I feel like this would be a great opportunity to use modern PHP features and gain autocompletion on the way. |
This feels like something that should be intentionally done to all properties, at least on Model, if not the entire framework (Job coming first to mind as another class that heavily uses properties). If I was a newbie, it would seem weird I could use attributes for PK, but not table name, or timestamp names, or on other classes. Then I'd wonder which is the "right" way, and if I can't consistently use Attributes for everything, I'd probably stick with the properties just for code consistency. (In general, I support switching to Attributes, but likely wouldn't switch over unless I could do it for just about everything) |
@pb30 If support for attributes is something that @taylorotwell is thinking about, I’d be happy to help with PRs adding that to other places in the framework |
Hi. How about inheritance...? I would vote no to this but I am sure it will get implemented just because... this is life:) |
I am not sure what your use-case would be here. As far as I know Laravel the old way would still be available.
Why not write all your code in assembly? We don't need that new language called C. ;-)
Yes.
Not sure what hints you are referring to. |
@Wulfheart UPDATE Attributes should be used only where they are the only solution in my opinion, not where there are better solutions. "I am not sure what your use-case would be here. As far as I know Laravel the old way would still be available." If you extend the model with a base model for example in which you put $incrementing to false via attributes, you need extra code to inherit that $incrementing false in the models that extend the base model if I am no mistaken. Update 2. |
@taylorotwell following the concern about having a lot of other PRs to add support for other Attributes, I'm thinking on releasing this and other attributes as a package, so we can check the adoption by the community and revisit this later on to see if it could be integrated to the core, what do you think? |
@WendellAdriel I can only speak for myself but I am a bit wary about using a package for this as it won't be maintained by the Laravel core team. Therefore, I think it would get a lot of adoption if there was a consistent first-party solution. |
@Wulfheart I understand, but a lot of super popular packages are also not maintained by the Core Team and used across thousands of projects. If this is something that the Core Team don't want to include and maintain in the core right now, using a package could be a good way to see the adoption and later on if the Core Team wants to add this to the Framework I'd be happy to help to bring this to the framework or even passing to them the ownership of the package if they want to make it a first-party package. cc: @taylorotwell |
Here's the package: https://github.com/WendellAdriel/laravel-virtue if someone wants to take a look or give it a try. |
The model has thousands of properties, so why would we just choose the primary key for this? An example of this is the |
@devajmeireles When I first created the PR I thought that would be a great idea. But if that's something that they don't want to add/maintain in the framework, it's totally understandable. |
I do think this needs to be part of a more all-encompassing approach to defining this type of stuff (properties vs. attributes). |
Changes
This PR adds a new attribute that can be used on Eloquent Models to allow defining the Primary Key for the Model in a really simple and intuitive way. This attribute is inspired by a package I created to play around with attributes and to know more about the Eloquent Models.
Why
This makes it really easy and intuitive to customize the primary key for the models.
Instead of this:
We can do this:
If we need to just update the name for example we can do this:
Additional Info
The package I mentioned has some other things on it and if this PR is something that the core team think could be useful I'll be happy to bring more ideas that I implemented on the package to be discussed to be added to the framework.