@-Layla- Hey, sure!
My comments were based on a first version of a multi-tenant CMS I had built. In it, I was automatically scoping models based on the domain name, but then this became a pain when I wanted to use my models in other situations like an admin panel, queue jobs, Artisan commands, etc. I’d constantly be adding queries, wondering why I wasn’t getting any results, and then forgetting the model would have the global scope trying to scope it to a (non-existent) domain name when ran outside of my application’s “main” web routes.
I rebuilt the CMS using a route group where I define the domain as a parameter. I look the website up by domain using route–model binding, and that then injects the Website model instance as the first parameter to controllers:
Route::domain('{website:domain}')->group(function () {
// Website routes here...
})->where(['website' => '.*']);
As controller actions get a Website instance injected, I can then access models through relations:
namespace App\Http\Controllers\Website;
class ArticleController extends Controller
{
public function index(Website $website)
{
// Access website articles via relation on Website model
$articles = $website->articles()->paginate();
return view('website::article.index')->with([
'website' => $website,
'articles' => $articles,
]);
}
}
I’m still using this approach today and works fine for me. It means I can use my models independently where ever I want to as well. For example, this will work as intended:
$draftEvents = Event::query()->draft()->get();
As it won’t try and scope it to a website like my previous version of the application did.