8

To display an error while using Laravel Artisan the official Laravel 5.8 documentation says:

$this->error('Something went wrong!');

But what's the context of $this?

Following is the contents of the seeder class file:

use Illuminate\Database\Seeder;

class PopulateMyTable extends Seeder
{
    public function run()
    {
        $this->info("Console should show this message");
    }
}
1
  • @porloscerrosΨ Got it, but how? Do I need to wrap about in Artisan::command? Commented Sep 12, 2019 at 20:50

3 Answers 3

19

You can do it by $this->command->method().

Where $this is this Seeder instance, command is this seeder console Command instance,
and method() could be any of the command available output methods.

<?php

use Illuminate\Database\Seeder;

use App\User;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $message = 'sample ';
        $this->command->info($message . 'info');
        $this->command->line($message . 'line');
        $this->command->comment($message . 'comment');
        $this->command->question($message . 'question');
        $this->command->error($message . 'error');
        $this->command->warn($message . 'warn');
        $this->command->alert($message . 'alert');
    }
}

Sign up to request clarification or add additional context in comments.

1 Comment

How to save this post?
1

Laravel 11 update:

In seeders, you can access the output components using:

$this->command->outputComponents()->info('info message');
$this->command->outputComponents()->success('success message');
$this->command->outputComponents()->error('error message');

2 Comments

This does not work.
@Programista How does it not work? I just rechecked it with my Laravel 11 app, the "11.37.0" version, to be exact, and it works just fine. Even the autocompletion works as expected when I type "$this->command->", and it shows "outputComponents()" as one of the options.
1

Now, the $command is defined as protected and it is not accessible through the seeders.

The $command is now defined as protected and is not accessible through the seeders.

May a simple PHP echo do the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.