Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a message in console using a seeder class in Laravel 5.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");
    }
}
like image 229
bart Avatar asked Sep 12 '19 20:09

bart


People also ask

How do you call a specific seeder in Laravel?

There are two ways by the help of which we can run a specific seeder file when we need. By using DatabaseSeeder. php file. By using –class flag in artisan command.

What is the use of seeder in Laravel?

Seeder or called Database Seeder is a feature of Laravel Php framework, which is used to auto generate data when an initial version is instantiated. We use a seeder to test our laravel project quickly and efficiently.


1 Answers

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');
    }
}

like image 111
porloscerros Ψ Avatar answered Nov 04 '22 20:11

porloscerros Ψ