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

nitiphone's avatar

Laravel, Dynamic table name base on user login

Dear All, As I try to create a system to be share use for my user. the logical as below when user login, they have to put system code like 1000,1001 then after login, all information will be special for their. as it have table tb_account_1000, tb_account_1001

if user login by 1001, then they will always use table tb_account_1001. Question: Any way we can do like this in model?

0 likes
2 replies
Snapey's avatar

easier to add a 'system_id` column to each table and then use a global scope to filter records

else, read about multi-tenant strategies

nitiphone's avatar

@Snapey Now I can use it below code $data = DB::table('tb_roomtype_' . session()->get('property_code'))->get(); return response()->json(['data' => $data]); but on join table or something is too long Could I create a global variable in controller ?

Please or to participate in this conversation.