Laravel | MySQL Database Connection Last Updated : 30 Dec, 2019 Comments Improve Suggest changes Like Article Like Report The database is an important element in any application. Laravel by default provides the support of MySQL. MySQL is a well known, open-source RDBMS (Relational Database Management System). Process for Connecting to the Database: Step 1: First we have to create a database. So, we will start Apache and MySQL server form XAMPP Control Panel. Step 2: Open any web browser, like Chrome, and type localhost/phpmyadmin in URL. Step 3: Now, click on the Databases tab and there, write the database named as geeksforgeeks and click on create. Step 4: Now, you will have to find a file named .env, where you will have to specify the details for the MySQL server, like database name, username, etc. In that file, you will have to search for names starting with DB_. Step 5: In that, you will find the DB_CONNECTION=mysql line. Below this are all the details specified for the database connection. You will have to specify the database name geeksforgeeks, that we created, after DB_DATABASE= and also specify username and password according to your need. Then save the file. Step 6: Now, we will create a View in resources/views directory with the name gfg.blade.php. Write the below code in the file. html <!DOCTYPE html> <html> <head> <title>GeeksforGeeks</title> <style> div { font-size: 22px; } </style> </head> <body> <div> <?php if(DB::connection()->getPdo()) { echo "Successfully connected to the database => " .DB::connection()->getDatabaseName(); } ?> </div> </body> </html> Step 7: Write the following route in the ‘web.php’ file in ‘routes’ directory. Route::get('gfg', function () { return view('gfg'); }); Step 8: Now, run the following Laravel artisan command to start the server: php artisan serve Step 9: And now, open the provided URL by the artisan in the browser with /gfg at the end. http://127.0.0.1:8000/gfg If the connection is successful then the message will appear as above or it will give an error as below: Custom Error Message: If you don’t want Laravel to handle it and give a predefined message then you can use try . . . catch block as shown below or can directlyreplace the step 6 codes with this lines of code: html <!DOCTYPE html> <html> <head> <title>GeeksforGeeks</title> <style> div { font-size: 22px; } </style> </head> <body> <div> <?php try { if(DB::connection()->getPdo()) { echo "Successfully connected to the database => " .DB::connection()->getDatabaseName(); } } catch (Exception $e) { echo "Unable to connect"; } ?> </div> </body> </html> Here, if unsuccessful then will print the message written in the catch block: Reference: https://laravel.com/docs/6.x/database Comment More infoAdvertise with us Next Article Laravel | MySQL Database Connection aakashpawar1999 Follow Improve Article Tags : Technical Scripter Web Technologies PHP Laravel Similar Reads PHP | MySQL ( Creating Database ) What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty, 3 min read Database Connection and Queries in Codeigniter To retrieve data from database we always need to connect our project with database. We will see how to connect database in codeignitor and how to run queries in it. In config folder we can find database.php file to connect with database. Mention the details as per shown image below. database.php In 3 min read Laravel | Installation and Configuration Laravel is a PHP framework that makes building complex web applications a cakewalk. In this guide you are going to learn how to Install and Configure Laravel and get your first Laravel project running in your computer. Prerequisites: Basic knowledge of PHPPHP InstallationBasic knowledge of Terminal/ 2 min read PHP mysqli_connect() Function The mysqli_connect() function in PHP is a fundamental tool for establishing a connection to a MySQL database. This function is crucial for PHP applications that need to interact with MySQL databases, enabling them to execute queries, retrieve data, and perform various database operations.In this art 3 min read How to prevent SQL Injection in PHP ? In this article, we are going to discuss how to prevent SQL injection in PHP. The prerequisite of this topic is that you are having XAMPP in your computer. Why SQL injection occurs? SQL injection is a code injection technique used to attack data-driven applications, in which malicious SQL statements 3 min read Like