Search code examples
phplaravelcommandlaravel-artisan

Writing Artisan Command - Display Data from Database


I want to write a Artisan command for my Laravel Web App.

The command should display the records from a specific table of a database.

I already created a new command called Loginstats.php in the Commands folder , but I'm struggling with the logic....

I think it's could be work with something in that way:

public function handle() {
        DB::table('logincount')->orderBy ( 'customer_id' )->chunk ( 100, function ($users) {
            foreach ($users as $user) {
                //
            }
        } );
    }

What do I need to do, to get the data from the database?

Is it also possible with the common artisan commands from the Command.php ?

Thank you!


Solution

  • Since DB is a facade, you should use full namespace:

    \DB::table...
    

    Or add use DB to the top of the class after namespace clause:

    use DB;