In this tutorial, You will learn how to display data from database in table using laravel you’ll need to follow a few steps. I’ll guide you through the process and provide a sample step.
In Laravel, you can display data from a database in a table by creating a controller to handle database queries, defining a Blade view to render the table, and setting up a route to access the data on a web page
fetch Data from Database in table in Laravel 10
before display data from database using laravel you should insert data into database To know more click on the link given below
https://codingstatus.com/insert-form-data-into-database-using-laravel/
1.Create A Controller
Create a controller using the Artisan command:
php artisan make:controller StudentController
Create a controller that will handle the logic for retrieving data from the database.In your controller (App\Http\Controllers/StudentCntroller.php), you can define a method like this:
Controller File: StudentController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { /** * Display a listing of the resource. */ public function index() { $students=Student::all(); return view('home', ['students' => $students]); } }
Declares a public method named index
. This method will handle the request to display a listing of students.
Retrieves all records from the Student
model and assigns them to the $students
variable. This assumes that there is a model named Student
representing the “students” table in the database.
sends the $students
variable to the ‘home’ view, making it accessible for display or manipulation in the view file.
2.Create A View
Create a Blade view file to display the data in Table. create a file named home.blade.php
in the resources/views
directory.
view File: home.blade.php
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel fetch data</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous"> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-sm-6"> <table class="table table-hover"> <thead> <tr> <th scope="col">ID</th> <th scope="col">Name</th> <th scope="col">City</th> <th scope="col">Marks</th> <th scope="col">Actions</th> </tr> </thead> <tbody> @foreach ($students as $stu) <tr> <th>{{$stu->id}}</th> <td>{{$stu->name}}</td> <td>{{$stu->city}}</td> <td>{{$stu->marks}}</td> <td> <a href="" class="btn btn-success btn-sm">Edit</a> <a href="" class="btn btn-danger btn-sm">Delete</a> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script> </body> </html>
This Blade view file creates a simple HTML page with a table to display student data
$students
: This assumes that $students
is an array or a collection of student records. Each element of this array or collection represents a student.
$stu
: In each iteration of the loop, the current student is represented by the variable $stu
. You can choose any variable name you prefer after the as
keyword.
3.Create Routes
In your web.php
routes file, define a route to your controller method:
Route File: web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; Route::get('/',[StudentController::class,'index'])->name('index');
Route::get('/')
: This is defining a route for the HTTP GET method.
[StudentController::class,'index']
: It specifies the controller and the method that should handle the request. In this case, it indicates that the index
method in the StudentController
class will handle the request.
->name('index')
: This assigns a name to the route, in this case, ‘index’. Naming routes is helpful for generating URLs and redirecting within the application.
4.Run The Development Server
php artisan serve
This will start the server, and you see the output of fetch data table.