How to create database schema (table) in Laravel?

后端 未结 5 1143
眼角桃花
眼角桃花 2021-02-09 07:53

I\'m new to Laravel.I\'m trying to create database in Laravel. I tried in console with:

Schema::create

5条回答
  •  孤独总比滥情好
    2021-02-09 08:18

    First you have to set database name,username and password in database.php in config folder.it look like

    'connections' => [
    
            'sqlite' => [
                'driver'   => 'sqlite',
                'database' => storage_path('database.sqlite'),
                'prefix'   => '',
            ],
    
            'mysql' => [
                'driver'    => 'mysql',
                'host'      => env('DB_HOST', 'localhost'),
                'database'  => env('DB_DATABASE', 'news'),
                'username'  => env('DB_USERNAME', 'root'),
                'password'  => env('DB_PASSWORD', ''),
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
            ],
    
            'pgsql' => [
                'driver'   => 'pgsql',
                'host'     => env('DB_HOST', 'localhost'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset'  => 'utf8',
                'prefix'   => '',
                'schema'   => 'public',
            ],
    
            'sqlsrv' => [
                'driver'   => 'sqlsrv',
                'host'     => env('DB_HOST', 'localhost'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset'  => 'utf8',
                'prefix'   => '',
            ],
    
        ],
    

    if you are using xampp then right click on your project folder and click on use composer here

    then run following command

     php artisan migrate:install
    

    and you can create table like

    php artisan make:migration create_users_table
    

    Migration Structure

    A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should simply reverse the operations performed by the up method.

    Within both of these methods you may use the Laravel schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder, check out its documentation. For example, let's look at a sample migration that creates a flights table:

    increments('id');
                $table->string('name');
                $table->string('airline');
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('flights');
        }
    }
    

    To know all artisan command run following command

    php artisan
    

    for more read following document http://laravel.com/docs/5.1/migrations

提交回复
热议问题