Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Garet's avatar
Level 3

Naming convention for models with relationships

Let's say I have a model called Users.

Then I have 2 related models called LoginHistory and Device.

LoginHistory records each login that a user makes, and Device keeps track of different devices/browsers that a user signs in with.

However I can't decide if to instead call my models UserLoginHistory and UserDevice.

Not having the prefix is more intuitive in terms of less typing.

However with the user prefix my models, and in particular the database tables, are grouped together alphabetically, and it's also more obvious at a glance that they're all related to a user.

I know there's no right or wrong answer, but I wondered if there was an overall consensus?

0 likes
6 replies
Jacey's avatar

Convention would dictate that you call the first model User, not Users.

I think I would have just the one additional model, LoginHistory, would would have a table that includes the user_id, device they logged in with and the datetime of the login. It avoids the duplication of the datetime of the login that would be present in both tables if you had both a LoginHistory and a UserDevce.

neeonline's avatar

I agree with @jacey

Later you can use Laravel's collection to return a list of all devices for the user, something like group by, etc.

Garet's avatar
Level 3

Thanks for the reply. The reason I separated Device into its own model/table is because I create a unique hash based on the browser, version and OS. If the user logs in using an unrecognised device they get a notification alerting them. Since they will most likely use the same device over and over it made sense to store the hash once in the Device table rather than several times in the LoginHistory table.

However back to the question, should it be Device and LoginHistory or UserDevice and UserLoginHistory ?

Jacey's avatar
Jacey
Best Answer
Level 5

I think UserLoginHistory is redundant. Since only users will be logging in, LoginHistory is plenty descriptive. If you had a model named Photo and there was only one kind of photo, that would be enough. If not, maybe you need a UserPhoto and a ProductPhoto, etc, so the differentiation makes sense. But not with logins.

Applying that same logic, is there more than one Device? People might say that it's logical to assume that nobody but a user will have a device. You don't need a UserDevice, DogDevice, CupcakeDevice? If not, Device makes sense.

Personally, I try to keep my model names as short as possible without being confusing because I'm lazy and don't like to type any more than necessary. A UserLoginHistory now needs a UserLoginHistoryController and boy does that get long.

neeonline's avatar

Use the short way. A device is a device. By default it belongs to an user.

In the future if the device belongs to more than one model (let's say a user and a company), you can use morph relations.

Garet's avatar
Level 3

Ok that answers my question then, I'll refactor and go with the shorter names. Thank you!

Please or to participate in this conversation.