ackuser
ackuser

Reputation: 5889

How to add fields to a model via eloquent in Laravel?

My question is if it is possible to add all the fields directly to a new model via Eloquent.

I guess it would be something like

php artisan make:model MyModel --fields=?

However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.

Thanks in advance

Upvotes: 3

Views: 7277

Answers (5)

Gonzalo
Gonzalo

Reputation: 410

I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like: https://github.com/Triun/laravel-model-base

It will read your database, and create the laravel eloquent models for you.

It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.

Basically it has 4 customization levels:

  • By out of the box modificators, configurable by the config files.
  • Including interfaces and traits to your auto-generated models.
  • Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
  • Generate the model base, but edit yourself the final model.

And, of course, suggestions and contributions are more than welcome.

Upvotes: 0

Imran
Imran

Reputation: 4750

If you mean table's column by fields then:

Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.

So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.

Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:

php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"

You can checkout their documentation for more information. Best of luck.

Upvotes: 2

Rama Durai
Rama Durai

Reputation: 750

No options while creating a model,

This is my terminal output (laravel 5.3) while i check,

modelmigration

You don't need to mention fields while creating model.

Ex:- based on the rules you should keep the names as like below,

model name as User
table name as users

then the model automatically handle everything, you don't need to mention the table/fields name.

Upvotes: 0

jeremykenedy
jeremykenedy

Reputation: 4285

it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs

There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:

https://github.com/laracasts/Laravel-5-Generators-Extended

It also looks like you can make a custom solution as well:

https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel

Hope that helps!

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163778

It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.

Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.

Upvotes: 0

Related Questions