【问题标题】:the use statement with non-compound name 'Auth' has no effect laravel具有非复合名称'Auth'的use语句没有效果laravel
【发布时间】:2017-09-11 06:50:12
【问题描述】:

我无法在我的播种机类中使用 Auth。我需要为我的 saas 应用程序获取tenant_id 变量。这是我的播种机类。

use Illuminate\Database\Seeder;
use App\AccountType;
use Auth;

class AccountTypeTableSeeder extends Seeder{

public function run()
{

    $accountType = new AccountType;
    $accountType->name = 'Travel Agent';
    $accountType->description = 'It is the description of the Travel Agent, so you write in detaisl about the account type.';
    $accountType->tenant_id = Auth::user()->tenant_id;
    $accountType->save();


}

}

我打了电话

$this->call(AccountTypeTableSeeder::class);

【问题讨论】:

  • 试试auth()->user()->tenant_id,反馈。
  • 同样的错误,“非复合名称'Auth'的use语句对laravel无效”。
  • 请发布完整文件
  • 文档说 $user = Auth::user(); // 获取当前认证的用户。但是由于您在播种机中,因此您没有经过身份验证的用户?
  • 我猜是这样,但我想使用 Artisan::call('db:seed'); 以编程方式调用我的播种机类;但无法使用 Auth 门面获取当前经过身份验证的用户tenant_id。

标签: php laravel


【解决方案1】:

在文件顶部放置:

use Illuminate\Support\Facades\Auth;

【讨论】:

  • 我做到了,我也尝试过,使用 Auth;两者都给出相同的错误。
  • 这对我有用,我在未登录时尝试在路由文件中使用Auth::check()。谢谢!
【解决方案2】:

因为我需要tenand_id,所以我使用最后插入的用户 id 并在以编程方式播种时获得了tenand_id。

Artisan::call('db:seed');

我用过

User::latest()->first()->tenant_id;

【讨论】:

    猜你喜欢
    • 2012-03-08
    • 1970-01-01
    • 2017-05-17
    • 2014-07-07
    • 1970-01-01
    • 2014-12-19
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多