How to get the table’s columns in Laravel?

If you ever wonder or came across a situation where you need to get the table’s columns from the database without even calling the model.

This is the way how you can easily get them.

use Illuminate\Support\Facades\DB;
use App\Models\User;

$columns = DB::getSchemaBuilder()->getColumnListing('users'); 

// or you can use model to get columns 

DB::getSchemaBuilder()->getColumnListing((new User())->getTable());

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.