Laravel 8 Tutorial - Start with Database
In this video, we are going to learn about database setup in Laravel 8. First, we need to make the necessary configurations.
To do this, we need to check two files. Let's switch to the project and go to the config folder. We need to check the database.php file, which contains a lot of configuration settings.
Let's take a look at the MySQL configuration. Here, we can see an array of MySQL settings, and the configuration is coming from an.env function. We don't need to put the database name directly, so let's take a look at the .env file.
Inside the project root directory, we can see the .env file. Let's open it and enter the database name, user, and password.
First, let's create a database. To do this, go to the browser and open http://localhost/phpmyadmin. Now, click on "Databases" and type in the database name. Let's say the database name is "laravel8prodb".
Now, switch to the project and enter the database name, username, and password in the .env file. The username is "root", and the password is blank, so leave it blank.
Save the .env file. After making changes to the .env file, we need to restart our application. To do this, switch to the command prompt and press Ctrl+C to stop the process. Then, type the following command:
php artisan serve
Now, let's create a controller. Inside the command prompt, run the following command:
php artisan make:controller PostController
Now, switch to the project and open the PostController file. Inside the PostController, create a function that fetches all posts from the database.
Before creating this function, let's create a table inside the database. Switch to phpmyadmin and create a table named "posts" with three columns: id, title, and body.
Now, let's create some posts. Click on "Insert" and type in the title and body of the post. Create one more post in the same way.
Now, switch to the project and inside the PostController, create a method to fetch the posts from the database. We will use Laravel's database query builder, which provides a convenient and fluent interface for creating and running database queries.
First, import DB at the top of the page:
use Illuminate\Support\Facades\DB;
Now, create a function to fetch all posts from the database:
public function getAllPost()
{
$results = DB::table('posts')->get();
return view('posts',['posts'=>$results]);
}
Now, let's create the posts view. Go to the resources folder and create a new file named posts.blade.php. Open this file and show all the posts inside the view:
<h1>Posts</h1>
@foreach($post in $posts){
<h3>{{$post->title}}</h3>
<p>{{$post->body}}</p>
@foreach
Now, create the routes for this. Go to the web.php file and type the following:
Routes::get('/posts','PostController@getAllPost');
Save the file. Let's check this. Switch to the browser and go to the URL localhost:8000/posts. You can see the posts with their title and description.
So, in this way, you can configure a database inside your Laravel 8 application.