3

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

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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!

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.