我正在构建一个多租户应用程序,我根据子域名区分租户。 我在laravel内核上注册了一个全局中间件,我需要在中间件中使用我的模型来获取数据库连接,然后将值分配给第二个mysql连接。
我试着按照文档的说法做些什么,但是有点像laravel我没有理解这个。
以下是我的中间件。 似乎是一个链接问题。
这是我的中间件。
<?php
namespace App\Http\Middleware;
use Closure;
class TenantIdentification
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function boot(Router $router)
{
parent::boot($router);
$router->model('tenant', '\App\Models\Tenant');
}
public function handle($request, Closure $next)
{
$tk = "HYD"; //hardcoded for the time being
$tenant = \App\Models\Tenant::where('tenantKey', $tk)->first();
var_dump($tenant);
exit();
return $next($request);
}
}
以下是我的模特。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tenant extends Model
{
protected $table = 'tenantinfo';
}
我得到了
TenantIdentification.php第28行中的FatalErrorException:Class “Class”App \ Models \ Tenant'未找到“。
第28行是$tenant = \App\Models\Tenant::where('tenantKey', $tk)->first();
我的模特位于app \ Models \ Tenant.php中 引导功能可以做什么吗?如果我可以在那里加载模型,我将如何在句柄方法中引用它?
答案 0 :(得分:1)
在模型文件中如何替换
namespace App;
与
namespace App\Models;
答案 1 :(得分:1)
在模型文件中,您使用 App
调用命名空间,并使用App\Models
引用它。
因此,请将模型文件中的命名空间更改为
namespace App\Models;