5

How would I get inside of a custom model the columns for a specific MySQL table using Eloquent in Laravel

I was trying DB::query("SHOW COLUMNS FROM " . $table) but without any success

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fefe
  • 8,755
  • 27
  • 104
  • 180

2 Answers2

5

What you are using is not Eloquent, but raw database queries.

RAW queries:

$table = 'your_table';
$columns = DB::select("SHOW COLUMNS FROM ". $table);
sidneydobber
  • 2,790
  • 1
  • 23
  • 32
0

There's no Eloquent function for this as it is considered out of its scope. I'd just do the following:

DB::select('show columns from '.(new MyModel)->getTable());
Andreas
  • 7,991
  • 2
  • 28
  • 37