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

Savlon's avatar

Multiple apps using the same models?

I have an existing laravel application that is for customers and I will be creating another application which uses the same database, however, it will be for administrators.

Rather than duplicating Models between packages, I want to be able to create one model and allow it to be used in both applications. I've done a bit of research and it appears the most common way is to add them to a custom composer package which is then required by both projects. This will allow me to write once and consume as many times as needed in any number of applications.

I've never done this before and I'm wondering if anyone with experience could give me some of the challenges and pitfalls when implementing it this way. I'm also open to any other ways people have tried and find better.

Cheers

0 likes
15 replies
tykus's avatar

We have done exactly this - a Models package that is a dependency of 3 different applications.

Obviously, you should have a solid test suite in each of the applications to ensure that updates in the Models package do not adversely affect the functionality of the application. We didn't have the concept of the different applications using different versions of the Models package; since the underlying database structure might change between versions. And we handled migrations/schema changes manually.

We served the package from a private Github repo, so required the server to have a token to fetch the package.

1 like
Savlon's avatar

@tykus Thanks for the reply. It's good to see that it's more than just a concept and is actually being used somewhere. All of the posts I read just suggested it as a solution, none actually stated they had implemented or worked with a structure like this.

Yeh we are the same. All applications will be using the same version of the models and anything else we need to throw in there such as seeders/factories to be used between both application tests.

What do you mean when you say you handled the database schema and migration changes manually? As in directly altered the table through a database client? If so, how did you run the tests between applications? They wouldn't be able to start from an empty database then would they?

Tray2's avatar

I would not build another app and somehow reuse existing models, I would just add an authorixation layer on the existing application, and use that to decide what to display or route to.

Savlon's avatar

@Tray2 Thanks for the reply! What do you mean by add an authorisation layer to the existing application? Do you mean implementing a pseudo multi tenant application or something?

Tray2's avatar

@Savlon It will not be multi-tenant but rather an admin layer to your application.

An administration dashboard if you will, just like @martinbean says.

martinbean's avatar

I have an existing laravel application that is for customers and I will be creating another application which uses the same database, however, it will be for administrators.

@savlon Why? Just build the administration panel in the same application, and use authorisation to restrict access to administrators only.

That way you don’t have two Laravel applications that you need to keep updated, or come up with “fixes” to a problem you’re entirely created yourself.

Savlon's avatar

@martinbean trust me, I have thought about that and even started to implement it. The problem with that is I would need to duplicate the project on separate servers and restrict one to admin and the other to customers. I would prefer if they were on separate servers for several reasons.

  1. Keep customer access and admin access separate.
  2. Allow resource intensive processes to run on either server without affecting the other.
  3. Reduce the size of the overall application. My customer front end is in vuejs so I would prefer not to have admin components for just about every component I have... and I don't want to convert my components into if else :is monsters which retrieve a component dependant on the user role.

This is a very large project and I want to keep it as clean as possible without adding code that could be added elsewhere.

Tray2's avatar

@Savlon Ok, this is my take on it

  1. f*** that, a user is a user and with the correct roles and authorizations it's not a problem. You can use admin prefixes on your routes to make it even more clear.
  2. That will not help you att all since you are using the same database and it's the database that does the heavy lifting, You can schedule jobs that uses a lot of resouces to run at times when the app isn't used or at least less used.
  3. Having two repositories to handle that, most likely with duplicate code is a worse option in my opinion. You don't need to have two versions of each component, just use the authorization to show and hide stuff on the components.
Savlon's avatar

@Tray2 you’re probably right but my inner ocd tells me to keep it separate lol!

The database will be mirrored with separate read/write streams so they’d be using the same database but different databases if that makes sense.

Yeh that is what I worry about. It would actually be 3 repos to manage because admin app, customer app and then any shared code app.

How do other companies handle this though? Surely a Facebook administrator doesn’t log into the same area a normal user does? Like I know I’m no where near their size and probably won’t ever be but to me it just seems wrong to allow an admin to log into the same application as a customer. I don’t know… I guess I have some thinking to do. Cheers

kokoshneta's avatar

The database will be mirrored with separate read/write streams so they’d be using the same database but different databases if that makes sense.

That sounds dangerous! Race conditions and deadlocks are bad enough in a single database – if you want to have users and admins working on out-of-sync mirror copies of the same database (and they will by definition be out of sync), you’ll just be adding to the potential pitfalls. What happens if a user does something that updates a row in a table, but at the same time an admin does something that deletes that row? You’ll need to handle that gracefully. The pitfalls are legion.

Surely a Facebook administrator doesn’t log into the same area a normal user does?

Why not? Facebook administrators are just regular Facebook users that have administrative rights over certain pages/groups/things. Or do you mean the people who work for Facebook and do behind-the-scenes administrative stuff? They probably have a separate admin panel-ish thing for that, yes, but there’s no reason it shouldn’t be part of the same overall application structure. For Facebook, of course, neither the databases nor the app itself is hosted on a single server, so they do need to handle all the pitfalls that come with that – but that’s because they need to serve billions of users every day.

Unless you know that a single server will not be able to cope with the number of users/traffic/resource usage you’re expecting, deliberately dividing your app into pieces spread across multiple servers generally has more downsides than advantages.

1 like
Tray2's avatar

@Savlon I agree with @kokoshneta that database mirroring isn't a good idea here, it should be used to keep two or ore databases in sync in case the main database (which handles all the io) fails, and then be switched to another one. You never update records in two places and expect it to work with out fucking up the database.

I suggest using KISS instead of overthinking it like you are doing now.

1 like
Savlon's avatar

@kokoshneta ok you and @tray2 have convinced me. I will need to organise my application incredibly well.

Thanks for the advice boys

S-Stephen's avatar

Hi @savlon,

How did you get on with this? I've done similar projects the way tray2 has suggested but keeping them completely separate provides so much more peace of mind.

I'm actually looking at a way with which I can re-use blade templates across applications as a want to write things with the same UI but have diverse and seperate user groups. I could implement this using authorization but each project/app has different stakeholders and change authority, from a business process point of view it will be far simpler to split the work up.

If you continued to investigate how to re-use code across apps I would be very interested. At the moment I'm considering version folders and symlinks rather than composer packages (I've come back to Laravel / PHP after 20+ years)

Please or to participate in this conversation.