Updated April 11, 2023
Introduction to Laravel Dashboard
Laravel uses Raw SQL or the Eloquent ORM to connect and interact with different database backends. The currently supported databases are MySQL, PostgreSQL, SQLite, SQL Server. The location of the database configuration of the particular application is config/database.php. It is here that you can specifically denote which database you want to keep for your default use and also change according to your needs. Laravel’s configuration can be easily modified according to the need of the database.
The database is needed to run the queries in the application which is being processed. The Raw SQL and Eloquent ORM provide the interaction with the database which is needed to implement the commands on the application to be processed.
How does Laravel Dashboard Works?
Below are the understanding and work of Laravel Dashboard:
1. Configuration
Below are the two configuration points:
SQLite Configuration
- Using the command touch database/database.sqlite, you can create a new SQLite database. You can later very conveniently configure the environment variables so that they can be pointed towards the newly created database. You need to use the absolute path of the database
Code:
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite
- The foreign_key_constraints option can be added to the config/database.php file to enable the foreign key constraints.
Configuration using the URLs
- The multiple configuration values which are used in the database are host, database, username, password, and similar values. Database connections are conveniently configured through them. There is a corresponding environment variable that pre-exists for each of these configuration values. When on a production server you are to manage multiple configuration values for the database, you will have to also manage multiple environment variables.
- Many managed database providers give a single database ‘URL’ which contains in it all information about the connection to the database in a single thread. Let us look at an example below.
Code:
mysql://root:[email protected]/forge?charset=UTF-8
- Laravel supports these kinds of URLs so that in a single thread all your database is linked to, instead of stressing on configuring the database you have along with multiple options of configuration. If the URL is present, it will be used so that you can remove credential information and the database connection.
2. Read and Write Connections
When you wish to use a single database connection for a select few statements, and a different one for the other executive statements like INSERT and DELETE, Laravel makes it possible for you. The correct connections will always be used in such different types of cases particularly as you are using raw queries or possibly the Eloquent ORM.
Code:
'mysql' => [
'read' => [
'host' => [
'192.168.1.1',
'196.168.1.2',
],
],
'write' => [
'host' => [
'196.168.1.3',
],
],
'sticky' =>true,
'driver' =>'mysql',
'database' =>'database',
'username' =>'root',
'password' =>'',
'charset' =>'utf8mb4',
'collation' =>'utf8mb4_unicode_ci',
'prefix' =>'',
Given above is an example of the configuration of read/write connections. You will notice that a single key host is the single key and has array vales for both read and write keys. The remaining database options for reading and write keys will be amalgamated from the main MySQL array.
The sticky option in the database
Being an optional value, it can be used when you wish to permit the quick and instant reading of the records which have been written in the database when the present request cycle would have been in process. When the sticky option is activated and the operation ‘write’ has also been performed in the present request cycle, in the latter stages whenever the ‘read’ option is used it will definitely use the ‘write’ connection.
3. Using Multiple Database Connections
There is the connection method that is available and you can use that whenever you want to use multiple connections. Through that, you can access each connection. There is the DB façade available through the connection method. One important thing to note here is that whatever name is passed to the method connection, it should match any of the different connections which are recorded in the config/database.php.File of configuration.
Code:
This is the code: $users = DB::connection('foo')->select(...);
4. Running the Raw SQL Queries
Using the DB façade, you can run the queries that you wish to run for yourself. Each type of query like the select, update, delete, etc. are provided with the DB façade. Let’s look at an example of running a select query:
Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
classUserControllerextendsController
{
/**
* Make the list of all the users of the application visible.
*
* @return Response
*/
publicfunctionindex()
{
$users = DB::select('select * from users where active = ?',[1]);
returnview('user.index', ['users' =>$users]);
}
}
The first and the main argument which has been passed to the select method is actually the raw SQL query. If there is any parameter binding which has to be bound to the concerned query, it is done in the second argument. Protection against the SQL injection is provided by the parameter binding.
5. Listening For Query Events
The listen method can be used when you want to receive each of the SQL queries which have been executed by the application you have. When you want to debug, this method is convenient. Below is the coding for registering the query listener in a particular service provider.
Code:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
classAppServiceProviderextendsServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
publicfunctionregister()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
publicfunctionboot()
{
DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
});
}
}
6. Database Transactions
The transaction method is used when you want to run a particular set of operations within a database transaction. If there is an exception within the Transaction which is Closure, There will be an automatic rolling back of the transaction. You don’t need to worry about manually rolling it back.
Conclusion
In the above explanation of the Laravel Database, we can easily understand the working of the Laravel Database and with the examples, we can see more clarity in the implementation of the different processes which are available with the database. It is used very extensively around the world and has good ratings by the users who have experienced its benefits.
Recommended Articles
We hope that this EDUCBA information on “Laravel Dashboard” was beneficial to you. You can view EDUCBA’s recommended articles for more information.