{"id":361,"date":"2023-12-19T14:31:57","date_gmt":"2023-12-19T08:31:57","guid":{"rendered":"http:\/\/itllive.com\/knowledgebase\/?p=361"},"modified":"2023-12-19T14:34:15","modified_gmt":"2023-12-19T08:34:15","slug":"basic-advanced-concepts-on-laravel-database-migrations-models","status":"publish","type":"post","link":"https:\/\/itllive.com\/knowledgebase\/2023\/12\/19\/basic-advanced-concepts-on-laravel-database-migrations-models\/","title":{"rendered":"Basic &#038; Advanced Concepts on Laravel Database Migrations &#038; Models"},"content":{"rendered":"<p><strong>Creating Migration File:<\/strong><\/p>\n<pre>php artisan make:migration create_<em>[table_name]<\/em>_table --table=<em>[table_name]<\/em><\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Example:<\/strong><\/span><\/p>\n<pre>php artisan make:migration create_<em>users<\/em>_table --table=<em>users<\/em><\/pre>\n<p><strong>Migration File for Updating Table:<\/strong><\/p>\n<pre>php artisan make:migration add_columns_to_<em>users<\/em>_table --table=<em>users<\/em><\/pre>\n<p><strong>Example of a Migration File:<\/strong><\/p>\n<pre>public function up()\r\n{\r\n    Schema::create('users', function (Blueprint $table) {\r\n        $table-&gt;id();\r\n        $table-&gt;string('name', 64); \/* second parameter defines length of a varchar field, it is optional *\/\r\n        $table-&gt;string('email')-&gt;unique(); \/* unique() makes the field unique *\/ \r\n        $table-&gt;timestamp('email_verified_at')-&gt;nullable(); \/* nullable() makes the field accept null values *\/\r\n        $table-&gt;string('password');\r\n        $table-&gt;rememberToken();\r\n        $table-&gt;timestamps();\r\n    });\r\n}\r\n<\/pre>\n<p><strong>Defining a Foreign Key:<\/strong><\/p>\n<pre>public function up()\r\n{\r\n    Schema::create('items', function (Blueprint $table) {\r\n        $table-&gt;string('code', 8)-&gt;primary(); \/* set as primary key manually *\/\r\n        $table-&gt;string('description')-&gt;unique();\r\n        $table-&gt;string('brand_code', 8);\r\n        $table-&gt;string('uom', 3)-&gt;default('PC'); \/* set a default value *\/\r\n        $table-&gt;boolean('is_active')-&gt;default(true);\r\n        $table-&gt;timestamps();\r\n\r\n        $table-&gt;foreign('brand_code')-&gt;references('code')-&gt;on('brands'); \/* defines brand_code as a foreign key to brands table *\/\r\n    });\r\n}\r\n<\/pre>\n<p><strong>Defining a Self Referencing Foreign Key:<\/strong><\/p>\n<pre>public function up()\r\n{\r\n    Schema::create('entities', function (Blueprint $table) {\r\n        $table-&gt;string('code', 12)-&gt;primary();\r\n        $table-&gt;string('description')-&gt;unique();\r\n        $table-&gt;string('parent')-&gt;nullable();\r\n        $table-&gt;boolean('is_active')-&gt;default(true);\r\n        $table-&gt;timestamps();\r\n    });\r\n\r\n    \/**\r\n     * Define the parent as self referencing key after the table is created\r\n     *\/\r\n    Schema::table('entities', function (Blueprint $table){\r\n        $table-&gt;foreign('parent')-&gt;references('code')-&gt;on('entities')-&gt;onUpdate('cascade')-&gt;onDelete('restrict');\r\n    });\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating Migration File: php artisan make:migration create_[table_name]_table &#8211;table=[table_name] Example: php artisan make:migration create_users_table &#8211;table=users Migration File for Updating ...<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":4,"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":365,"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/361\/revisions\/365"}],"wp:attachment":[{"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itllive.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}