Ad

How Do You Know What Fields Are In A Laravel Model?

- 1 answer

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?

Ad

Answer

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()));
Ad
source: stackoverflow.com
Ad