May Sale! All accounts are 40% off this week.

SangminKim's avatar

Artisan make:model with auto filling $table and $fillable

Hi, I wonder if there's an option for artisan make:model that allows me to grab a table to autofill everything in the created model class.

Well...something like this...

user> php artisan make:model Category --table:category --autofill.

Ta-da !!

class Category extends Model
{
    /**
     * The database table used by the model. -- this is auto generated
     *
     * @var string
     */
    protected $table = 'category';

    /**
     * The attributes that are mass assignable. -- this is auto generated
     *
     * @var array
     */
    protected $fillable = ['id', 'category', 'created_at', 'updated_at'];
}

I've been looking for something like this... but no luck. Does such options exist anyway?

Thanks!!!

0 likes
3 replies
bimalshah72's avatar
Level 6

@SangminKim In the same artisan command no such options exits.

If you want to see what options available in this commnad,

use

user> php artisan help make:model

Of course, you can create your own custom command for your requirement, but in make:model , no way

SangminKim's avatar

Yup... I already looked into options using 'help' option but couldn't find any related info.

I guess i'm being too lazy here :(

Thanks!

salmon's avatar

You can use a combination of JeffreyWay's Laravel-5-Generators-Extended https://github.com/laracasts/Laravel-5-Generators-Extended to create these models dynamically - even populating the columns and types in the artisan command.

And then you can create some entries in your model factory using faker.

$factory->define(User::class, function (Faker\Generator $faker) {
    return [
        'created_at' => $faker->dateTime(),
        'updated_at' => $faker->dateTime(),
        'deleted_at' => $faker->dateTime(),
        'first_name' => $faker->firstName,
        'last_name' => $faker->lastName,
        'email' => $faker->email,
        'language_id' => $faker->numberBetween(1, 4),
        'password' => app('hash')->make($faker->password)
    ];
});

now you can just call this in Tinker or in a seeder file.

This line will create 100 new users.

      factory(User::class, 100)->create();
1 like

Please or to participate in this conversation.