i am building a laravel multi tenant (each tenant have a seperated db), with tenancyforlaravel package. Now i am building a structure where users can create it own entity like: Cars, Jobs etc. I have done this successfully on the central DB, but am facing a problem when i do this structure for tenants. First, I create the relevant fields that will be part of an entity, then I create the entity and add the fields I just created, at this moment migrations, models, controllers, resources are created and a db table looks like this:
select * from entities;
+----+------+-------+-----------+---------------------+---------------------+
| id | name | order | is_active | created_at | updated_at |
+----+------+-------+-----------+---------------------+---------------------+
| 1 | jobs | 0 | 1 | 2024-05-23 11:51:51 | 2024-05-23 11:51:51 |
+----+------+-------+-----------+---------------------+---------------------+
This is on tenant db. NOTE: each tenant db has this prefix: tenant
and the full table name will be tenant+profilename, in my case: tenantagency
;
So far so good. The problem comes when i try to add routes for this entity, and on practice i so something like this:
// tenant.php
Route::middleware([
'web',
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class,
InitializeTenancy::class,
])->group(function () {
....
$tenant->get('entity', $baseViewController);
$tenant->get('fields', $baseViewController);
// Include custom web entity routes
require __DIR__.'/tenant/entities/entityRoutesWEB.php';
....
});
// .../entityRoures/php
$entities = Entities::all();
// dd($entities);
foreach ($entities as $entity) {
$entityName = strtolower($entity->name);
$controllerName = 'AppHttpControllersAPITenant\' . ucfirst($entityName) . 'Controller';
// Define routes for this entity
Route::get("/{$entityName}", [BaseViewController::class, 'index']); // Fetch data for the entity
}
So my problem is here: `$entities = Entities::all();
// dd($entities);` this is getting the entities from the central db.
// /Tenant/EntitiesModel.php
namespace AppModelsTenantEntities;
// /Central/EntitiesModel.php
namespace AppModelsCentralEntities;
NOTE: Same logic i have used for the central DB and it work.