Database connection in Laravel
Database connection establishing and getting data from DB
In these notes, you can understand establishing a DB connection and retrieving data from DB.
Last previous notes, we made blog post data by hard coding, now fetching data from MySQL database.
Here I have created a MySQL database and table for blogs.
Now I am going to add MySQL database configuration in Laravel. As we discussed on early notes we should config DB in .ENV file
After this setup, I need to write a method in my controller to fetch data from DB.
Example 1
Getting data from DB, This is simple getting data from DB. I used use DB; class and I written direct query.
Example 2
Using models, Laravel has Eloquent feature.
You can create a model using PHP artisan commands
php artisan make:model model_name
In my view, I simple print the blog_desc
and in web.php
Result same as we expected
Comments
Post a Comment
Thank you :)