Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could two projects use same database?

The question might sound vague but in fact, it's something I've been wondering for some time and I don't know how can I do it.

I've been working with Laravel 5 for a while, but most of the projects I've developed using Laravel, have the frontend app and the backend app in the same project This time the requirement is to make two separate projects, one for the frontend, and the other one for the backend.

The inquiere comes up when trying to figure out how will the controller interact with the Model, and how will the migrations be handled? Will I have to make a model for each table in both the frontend and the backend?

The goal is to use the same database for both projects, without having problems with the migrations and the models.

EDIT 1 What i mean by Frontend App is the website where the data will be displayed, where people will interact, where they will see something. The Backend App is the one where the admins will edit the content of the frontend, where they will be able to CRUD to the database; in the frontend app i just want to select data from db.

like image 336
Jonathan Solorzano Avatar asked Jun 07 '26 19:06

Jonathan Solorzano


1 Answers

I had this same issue, and thought I'd answer for anyone in future who may wish to do same. There are many cases where you may have two separate apps that need to draw from the same database. In my case, it was a backend that input various data sets, and a frontend/client-facing site that clients drew information from a specific area. Both were huge apps, and having them separate was a benefit.

The magic comes from a few very simple lines in composer surrounding PSR4 namespacing.

If you know in advance of creating both projects which models you want to share, you can simply create a separate folder and put all of these models into namespace 'Share' or somesuch. Then pull them down into both apps via composer's autoload in same way as below.

If, like me, you have a project already completed (and don't want to change namespacing at this point in the original) and NOW want to add another project that uses many of the same models, here's what I did:

  • Create new Laravel project
  • Set new namespace on new project as something different than 'App'

    php artisan app:name NewName

  • Make sure to do this step first! If you open files, or make changes, the auto-re-namespace doesn't work.

  • Add instructions in Composer.json to look for all old models (and anything else) via the namespace "App", and all models of the current app under NewName (NOTE you can go above the root install folder):

"autoload": { "classmap": [ "database" ], "psr-4": { "NewName\\\": "app/", "App\\\": "../original-project-folder/app/" } }, - Because migrations may depend on a local class model, you need to keep migrations for the new project in the new project folder. You can run these after the original migration and it works fine.

  • If, like me, you have a different set of users for app#2, and want to change the User model (so as not to use the original app User class), this is a bit more work, mainly around authorization. Make changes to app/config/auth.php to ensure the right class name is available for your new user class.

Took me forever to figure this out - it's not easily searchable. Hope this helps someone.

like image 62
Watercayman Avatar answered Jun 10 '26 11:06

Watercayman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!