我仍然是Laravel的新手,并且已经完成了一些基本的laracasts.现在我开始了我的第一个laravel项目,但我仍然坚持如何使用我的第一个包“Landlord”.基本上我需要在我的应用程序中设置多租户.我有一个公司表和一个用户表,用户表有一个company_id列.当公司注册成功创建公司并将company_id附加到用户时.
我认为Landlord是实现多租户应用程序的最佳方式,所以我完成了安装说明,现在我将它包含在我的应用程序中.
然而,USAGE部分的第一行说:
重要提示:房东是无国籍人.这意味着当您调用addTenant()时,它只会调用当前请求的范围.
Make sure that you are adding your tenants in such a way that it
happens on every request, and before you need Models scoped, like in a
middleware or as part of a stateless authentication method like OAuth.
看起来我需要附加一个Landlord :: addTenant(‘tenant_id’,1);正面.
这可能是一个非常简单的答案我忽略了但是哪里是使用addTenant的最佳地点,我是否必须使用每个控制器或模型重新声明它?我应该在用户登录时附加它,在我的路线中使用它还是用作中间件?如果它是一个中间件,则以下是正确的,以便从当前用户中提取company_id并将其与addTenant一起使用?
中间件:
public function handle($request, Closure $next){
$tenantId = Auth::user()->tenant_id;
Landlord::addTenant('tenant_id', $tenantId);
return $next($request);
}
UPDATE
这是我的中间件(MultiTenant.php)
<?php
namespace App\Http\Middleware;
use Closure;
use App\User;
use Illuminate\Support\Facades\Auth;
class MultiTenant
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::check()) {
$tenantId = Auth::user()->company_id;
Landlord::addTenant('company_id', $tenantId); // Different column name, but same concept
}
return $next($request);
}
}
我的路线/ web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::group(['middleware' => ['multitenant']], function () {
Route::get('/home', 'HomeController@index');
//Clients
Route::resource('clients', 'ClientController');
});
我的Client.php模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use HipsterJazzbo\Landlord\BelongsToTenants;
class Client extends Model
{
use BelongsToTenants;
//
protected $fillable = [
'organization',
];
}
https://github.com/HipsterJazzbo/Landlord#user-content-usage
解决方法:
虽然只有一个选项,但我也选择了中间件路线.我认为这是实现它的简单方法.
我将中间件添加到我的routes / web.php文件中:
Route::group(['middleware' => ['landlord']], function () {
// Your routes
});
我的房东中间件看起来像这样:
public function handle($request, Closure $next)
{
if (Auth::check()) {
$tenantId = Auth::user()->company_id;
Landlord::addTenant('company_id', $tenantId); // Different column name, but same concept
}
return $next($request);
}
然后我只是将特征添加到我想要作用域的模型中:
use HipsterJazzbo\Landlord\BelongsToTenant;
class User extends Authenticatable
{
use BelongsToTenant;
}
更新
另外,请确保在config / app.php文件中已将landlord添加到providers数组中:
'providers' => [
// ...
HipsterJazzbo\Landlord\LandlordServiceProvider::class
// ...
],
对于别名数组:
'aliases' => [
// ...
'Landlord' => HipsterJazzbo\Landlord\Facades\Landlord::class,
// ...
],
然后在完成后最终使用composer dump-autoload刷新自动加载.
标签:php,laravel,laravel-5-2 来源: https://codeday.me/bug/20190627/1309075.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。