Setting up your database and tables in Laravel 5

来源:互联网 时间:1970-01-01

In this article, we will show you how to set up your database, use migrations in Laravel 5 and get help for artisan commands.

Setting up the database

Once you have Composer set up and have created a Laravel project in some folder you would have to set up your database and tables. You can create a new Laravel project with Composer by typing composer create-project laravel/laravel in your Terminal/Command Prompt after having navigated to the folder where you want Laravel to be set up (You would need Composer set up in your PATH environment variable to use it from any folder in your Terminal).

Next, you would need to open the .env file which is located in the root of the project. Inside that file, you would need to set up your environment variables related to your database.

Those are my database settings in the .env file:

DB_HOST=localhostDB_DATABASE=blogDB_USERNAME=rootDB_PASSWORD=***** DB_HOST=localhostDB_DATABASE=blogDB_USERNAME=rootDB_PASSWORD=*****

You can also check the config/database.php file and see or change the default settings related to your database. The default settings will be implemented if no .env file exists or no such property exists in it. For example, when you have the following lines in the database config file

'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'mysql'=>[ 'driver' =>'mysql', 'host' =>env('DB_HOST','localhost'),

The host would be set to the DB_HOST property that you set in your .env file. If no such property exists in there, then the value localhost would be used.

'default' => env('DB_CONNECTION', 'mysql'), 'default'=>env('DB_CONNECTION','mysql'),

If you cannot find a DB_CONNECTION property in the .env file and you see the line above, then your application is using a MySQL database. Therefore, if you want to use SQLite instead or a different database you could either add a new property named DB_CONNECTION=sqlite in your .env file or just change mysql in config/database.php .

You should also set the public folder to be the landing root for the web server.

Migrations

You do not have to manually create your table. Start a migration for the new table by typing php artisan make:migration create_blog_table –create=blog in your Terminal. This will create a template for creating a new table called blog . You can find that migration file inside the database/migrations folder.

Below is the migration file that resulted from our command. The only thing we have added is a string (VARCHAR) field which we called title and a text (TEXT) which we called content .

The $table ->timestamps method adds the timestamps (created_at and updated_at) which Laravel manages by itself. Whenever you add a new item or update an existing one in your database, Laravel will update those timestamps.

<?phpuse Illuminate/Database/Schema/Blueprint;use Illuminate/Database/Migrations/Migration;class CreateBlogTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('blog', function (Blueprint $table) { $table->increments('id'); $table->string("title"); $table->text("content"); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('blog'); }} <?php useIlluminate/Database/Schema/Blueprint;useIlluminate/Database/Migrations/Migration; classCreateBlogTableextendsMigration{ /** * Run the migrations. * * @return void */ publicfunctionup() { Schema::create('blog',function(Blueprint$table){ $table->increments('id'); $table->string("title"); $table->text("content"); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ publicfunctiondown() { Schema::drop('blog'); }}

Now, all we have to do is type php artisan migrate in our Terminal and the table with all entered fields will be created.

Actually, the up method is the action that you want to perform when implementing the migration and down is the method that you want to implement when reverting the change brought by the migration.

Therefore, if you create a new table within the up method you would drop the table in the down method. Similarly, if you add a column in the up method, you remove the column in the down method.

You can return your database to its state prior to the last migration by typing the command php artisan migrate:rollback . The command will execute the down method for the last migration.

Help in learning Artisan commands

Artisan provides a lot of help commands. You can use php artisan list to see all currently available commands. You can type php artisan help commandName to see what kind of arguments a command accepts, how you can use the command and what it does.

The image below provides an example of getting help for rollbacks of migrations:



相关阅读:
Top