Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wangchen's avatar

the use statement with non-compound name 'Auth' has no effect laravel

I am unable to use Auth in my seeder class. I need to use tenant_id for my saas application. Here's my seeder class.

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();

}

}

0 likes
5 replies
tykus's avatar

You don't need this:

use Auth;

It is helpful to think of this as aliasing rather than importing.

martinbean's avatar

@wangchen Yeah, you don’t need to import Auth because it’s in the global namespace, and seeders also live in the global namespace, so the import is redundant.

wangchen's avatar

@tykus and @martinbean if i don't import then m getting the error, "Trying to get the property of non-object". Since i needed the tenand_id, i did it with the last inserted id of the user and got the tenant_id when seeding programmatically.

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

i used instead the User model

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

martinbean's avatar
Level 80

@wangchen You’re not going to have an authenticated user in a seeder class. A user doesn’t “log in” when you running seeds.

Please or to participate in this conversation.