martinszeltins's avatar

How can I know what fields I have on my Model without looking at my database?

Having worked with Symfony, I have a question about Eloquent Models. In Symfony all the attributes are right there in the Model/Entity class so you get auto-completion and you know what fields your Model has without having to open your database and look at the columns.

Symfony:

class User extends ...
{
    private $username;
    private $password;
    private $street_address;
    private $phone_number;
    private $created_at;
    private $updated_at;
}

But in Laravel you do not see these fields on Model itself.

class User extends Model
{
    
}

Which means you get no auto-completion and you do not know what fields you have unless you open your database, go to the table and look at the columns. Or you have to trace it through all the different migrations for that table.

Or am I missing something here?

0 likes
12 replies
lbecket's avatar

I will often use Tinker to look at the first record...


php artisan tinker

>>> User::first();

lbecket's avatar

@martinszeltins Indeed... I was just saying that I often find Tinker to be the fastest reference. Maybe copying them from your migration as comments into your model would cause your IDE to pick them up. Just spit-balling here...

jlrdw's avatar

@martinszeltins I didn't think it was:

DB::getSchemaBuilder()->getColumnListing('table_name_here');

Edit: Okay you want them somehow auto completed. I thought you meant listing of columns..

1 like
laracoft's avatar

@martinszeltins I think you can rephrase your title to get better answers:

One I can think of is Auto-complete for Model attributes

Sinnbeck's avatar

@martinszeltins Honestly I havent tried just adding private properties.. So you can give that a try. Just tested it quickly and it worked fine. So maybe you can do it just the same if you prefer (you just cannot use the property names that are already in use)

Please or to participate in this conversation.