How to get data form database using Laravel 6

How to get data form database using Laravel 6


 

How to Retrieve Data from a Database in Laravel 6

In Laravel 6, you can fetch data from a MySQL database using Eloquent ORM and Query Builder. Let’s go through both methods step by step.

1. Using Eloquent ORM (Recommended)

Eloquent makes it easy to fetch data with clean syntax.

Step 1: Create a Model (If Not Created)

php artisan make:model Product

This creates app/Product.php.

Step 2: Define the Model

Modify app/Product.php:

namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $table = 'products'; // Define table name }

Step 3: Fetch Data Using Eloquent

a) Get All Records

use App\Product; $products = Product::all();

b) Get a Single Record by ID

$product = Product::find(1);

c) Get Specific Columns

$products = Product::select('name', 'price')->get();

d) Where Clause (Filter Data)

$products = Product::where('price', '>', 500)->get();

e) Order Data

$products = Product::orderBy('price', 'desc')->get();

f) Paginate Data

$products = Product::paginate(10);

2. Using Laravel Query Builder

Query Builder is a lightweight way to fetch data without using models.

Step 1: Import DB Facade

use Illuminate\Support\Facades\DB;

Step 2: Fetch Data

a) Get All Data

$products = DB::table('products')->get();

b) Get a Single Record by ID

$product = DB::table('products')->where('id', 1)->first();

c) Select Specific Columns

$products = DB::table('products')->select('name', 'price')->get();

d) Use Where Clause

$expensiveProducts = DB::table('products')->where('price', '>', 500)->get();

e) Order Data

$products = DB::table('products')->orderBy('price', 'desc')->get();

3. Fetch Data in a Controller and Display in a Blade View

Step 1: Create a Controller

php artisan make:controller ProductController

Step 2: Fetch Data in Controller

Modify app/Http/Controllers/ProductController.php:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Product; class ProductController extends Controller { public function index() { $products = Product::all(); // Fetch all products return view('products.index', compact('products')); } }

Step 3: Create a Blade View

In resources/views/products/index.blade.php:

<!DOCTYPE html> <html> <head> <title>Product List</title> </head> <body> <h2>Product List</h2> <table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Price</th> </tr> @foreach ($products as $product) <tr> <td>{{ $product->id }}</td> <td>{{ $product->name }}</td> <td>{{ $product->price }}</td> </tr> @endforeach </table> </body> </html>

Step 4: Define Route

In routes/web.php:

use App\Http\Controllers\ProductController; Route::get('/products', [ProductController::class, 'index']);

Conclusion

Eloquent ORM (Recommended for easy, readable queries)
Query Builder (Useful for SQL-like flexibility)
Controller & View (For displaying data on a web page)

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close