Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you know what fields are in a Laravel Model?

Tags:

php

laravel

I am trying to unit test creating a company however I don't know what the fields/attributes of the model are.

So I look in App\Company.php, but there is no list of fields there.

Then I look at the migrations, but I have to go through each of them to find the fields available.

So as a last resort I open a DB explorer to find what fields are in the model.

Is there an easier way to know what fields exist in a model?

like image 420
tread Avatar asked Jan 06 '23 23:01

tread


1 Answers

You can do it this way, without the need of loading any object from the db:

$fields = (new \App\Company())
->getConnection()
->getSchemaBuilder()
->getColumnListing((new \App\Company())->getTable());

Also you can:

$fields = Schema::getColumnListing((new \App\Company())->getTable()));
like image 85
Amarnasan Avatar answered Jan 15 '23 08:01

Amarnasan