在 laravel 中访问中间件中的模型
Accessing model in middleware in laravel
我正在构建一个多租户应用程序,我正在根据子域区分租户。
我在 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';
}
我明白了
"FatalErrorException in TenantIdentification.php line 28: Class
'Class 'App\Models\Tenant' not found".
第 28 行是 $tenant = \App\Models\Tenant::where('tenantKey', $tk)->first();
我的模型位于app\Models\Tenant。php
启动功能有什么作用吗?如果我可以在那里加载模型,我将如何在 handle 方法中引用它?
在模型文件中如何替换
namespace App;
和
namespace App\Models;
在模型文件中,您使用 just App
调用命名空间,并使用 App\Models
.
引用它
因此,将模型文件中的命名空间更改为
namespace App\Models;
我正在构建一个多租户应用程序,我正在根据子域区分租户。 我在 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';
}
我明白了
"FatalErrorException in TenantIdentification.php line 28: Class 'Class 'App\Models\Tenant' not found".
第 28 行是 $tenant = \App\Models\Tenant::where('tenantKey', $tk)->first();
我的模型位于app\Models\Tenant。php 启动功能有什么作用吗?如果我可以在那里加载模型,我将如何在 handle 方法中引用它?
在模型文件中如何替换
namespace App;
和
namespace App\Models;
在模型文件中,您使用 just App
调用命名空间,并使用 App\Models
.
因此,将模型文件中的命名空间更改为
namespace App\Models;