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

ITProperty's avatar

How to include Schema builder in a Controller?

Hi Guys I have the following code,

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class SampleController extends Controller
{
    public function index()
    {       
        //Use Schema builder here
        $columns = Schema::getColumnListing('users');
    }
}

I get a Class 'App\Http\Controllers\Schema' not found error when running it. I understands that the the default namespace is App\Http\Controllers for controller so thats why it looks for the class in that folder. So then how do I properly include the laravel Schema Builder?

As you can see I've tried

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

Like a migration file does but its no use.

Any help would be greatly appreciated!

Thanks

0 likes
3 replies
bestmomo's avatar
bestmomo
Best Answer
Level 52

Add this :

use Illuminate\Support\Facades\Schema;
1 like
Snapey's avatar

Try use Illuminate\Support\Facades\Schema

edit: drat!

1 like
Kamfu's avatar

Hi @bestmomo @snapey

I searched a solution from stackoverflow, but which one work better? any performance concert?

public function getTableColumns($table)
{
    return DB::getSchemaBuilder()->getColumnListing($table);

    // OR

    return Schema::getColumnListing($table);

}

Please or to participate in this conversation.