D
Engineering

Multi-tenant

Data between merchants is separated by store_id. Queries without store_id scope leak data across merchants.

dazoapp is multi-tenant. Merchant data is separated by store_id (795 usages). Queries without scope = data leak across merchants.

php
// CORRECT
$bank = Bank::where('store_id', $store->store_id)->get();

// WRONG — leaks to all merchants
$bank = Bank::all();

Key points (see Indonesian version for details):

  • Get $store_id via auth()->user()->user_store->store_id — works for all roles (owner, CS, admin)
  • Do NOT use $user->store — it only works for owners (returns null for non-owners)
  • Always go through the UserStore pivot table, even for owners, for consistency
  • Exception: if you already have store_id from request/context, query Store directly
  • Eloquent relations do not inherit tenant scope — always scope explicitly
  • In Jobs/Observers/Commands, pass store_id explicitly
  • Never use Model::all() or Model::first() for tenant models