
How to get column names from table in Laravel
How to get column names from table in Laravel
We’ll attempt to use programming in this lesson to solve the "How to get column names from table in Laravel" puzzle.
You can get all the column names from a table using the DB facade and Schema facade. You have to call the getColumnListing() method (using DB and Schema facade) by passing the table name as an argument to get all columns from the table.-
Laravel get column names from a table
--PATH routes\web.phpRoute::get('/get-table-columns', function(){ $table = 'users'; return DB::getSchemaBuilder()->getColumnListing($table); });
Output:
["id","name","email","email_verified_at","password","remember_token","created_at","updated_at"]
//Methods you can use to get the list of columns from a table.
Schema::getColumnListing($table);
DB::getSchemaBuilder()->getColumnListing($table);
You can easily use facade (Schema and DB facade) using use statement. .
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB;
You have to copy/paste code in routes\web.php and visit URL http://localhost:8000/get-table-columns to get all column (field) names of a specified table.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Get current URL on visit URL in Laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- How to change default timestamp fields name in Laravel
- Laravel file size validation not working
- Page loader in laravel
- Route not defined in Laravel
- How to Get records between two dates in Laravel
- Eager loading dynamically in laravel
- Target class [HomeController] does not exist
- Class 'App\Rules\Hash' not found in Laravel
- How to return error message from controller to view in laravel
- Add class to body in laravel view
- Save or update pivot table data with additional column in Laravel
- Laravel change date format
- How to get IP address in laravel
- Insert Comma Separated Values in laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- If no route matched route::fallback in laravel
- How to create controller in laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- How to pass two variables in HREF in laravel
- How to include header file in laravel
- Include External CSS and JS file in Laravel
- First and last item of the array using foreach iteration in laravel blade
- How to get records in random order in laravel