Examples of creation of tables via make on Laravel

WINW > Software Development > Laravel > Examples of creation of tables via make on Laravel
  • String and Nullable Field:
    • We want a name column to store product names. It should be a string and nullable (i.e., it can be empty).
php artisan make:migration create_products_table --create=products --schema="name:string:nullable"
  • Integer Field with Default Value:
    • We’ll add an quantity column to store the product quantity. It’s an integer with a default value of 0.
php artisan make:migration create_products_table --create=products --schema="name:string:nullable, quantity:integer:default(0)"
  • Boolean Field:
    • Let’s include an is_available column to indicate whether the product is currently available (true or false).
php artisan make:migration create_products_table --create=products --schema="name:string:nullable, quantity:integer:default(0), is_available:boolean"
  • Date Field:
    • We’ll add a release_date column to store the product release date.
php artisan make:migration create_products_table --create=products --schema="name:string:nullable, quantity:integer:default(0), is_available:boolean, release_date:date"
  • Enum Field:
    • Suppose we want a category column to represent product categories (e.g., “Electronics,” “Clothing,” “Books”).
php artisan make:migration create_products_table --create=products --schema="name:string:nullable, quantity:integer:default(0), is_available:boolean, release_date:date, category:enum('Electronics', 'Clothing', 'Books')"
  • JSON Field:
    • We’ll add a metadata column to store additional product information in JSON format.
php artisan make:migration create_products_table --create=products --schema="name:string:nullable, quantity:integer:default(0), is_available:boolean, release_date:date, category:enum('Electronics', 'Clothing', 'Books'), metadata:json"

Remember to adjust the field names and types according to your specific use case. After creating the migration, run php artisan migrate to apply it to your database. Happy coding!

For more details, you can refer to the official Laravel documentation on migrations 

Leave a Reply