Retrieve data from database using Laravel

Retrieve data from database using Laravel

Step 1: Install Laravel 7 Project

Run the following command in your terminal:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Set Up Database Connection

Update your .env file with these credentials:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=management_system DB_USERNAME=root DB_PASSWORD=123456

Step 3: Define Routes in web.php

// ------------------------ form ------------------------ // Route::get('viewStudent', 'StudentController@viewStudent')->name('viewStudent'); Route::post('/insertStudent', 'StudentController@insertStudent'); Route::get('reportStudent', 'StudentController@reportStudent')->name('reportStudent');

Step 4: Controller - StudentController.php

Add this method:

public function reportStudent() { $user = Auth::user(); $reportStudent = DB::table('students')->get(); return view('report.report_student', compact('user', 'reportStudent')); }

Step 5: Create Blade View - resources/views/report/report_student.blade.php

@extends('layouts.master') @section('menu') @include('sidebar.report_student') @endsection @section('content') <div class="pcoded-content"> <div class="pcoded-inner-content"> <div class="main-body"> <div class="page-wrapper"> <div class="page-header"> <div class="row align-items-end"> <div class="col-lg-8"> <div class="page-header-title"> <div class="d-inline"> <h4>Table Report</h4> <span>Report display all</span> </div> </div> </div> <div class="col-lg-4"> <div class="page-header-breadcrumb"> <ul class="breadcrumb-title"> <li class="breadcrumb-item"><a href="index-1.htm"><i class="feather icon-home"></i></a></li> <li class="breadcrumb-item"><a href="#!">User Table</a></li> <li class="breadcrumb-item"><a href="#!">Back home</a></li> </ul> </div> </div> </div> </div> <div class="page-body"> <div class="row"> <div class="col-sm-12"> <div class="card"> <div class="card-header"> <h5>Student Report</h5> <span>Report total</span> </div> <div class="card-block"> <div class="table-responsive dt-responsive"> <table id="dom-jqry" class="table table-sm table-striped table-bordered nowrap"> <thead> <tr> <th>ID STUDENT</th> <th>NAME</th> <th>SEX</th> <th>AGE</th> <th>EMAIL</th> <th>PHONE</th> <th>ACTION</th> </tr> </thead> <tbody> @foreach ($reportStudent as $item) <tr> <td class="idStudent">{{ $item->id_student }}</td> <td class="name">{{ $item->name }}</td> <td class="sex">{{ $item->sex }}</td> <td class="age">{{ $item->age }}</td> <td class="email">{{ $item->email }}</td> <td class="phone">{{ $item->phone_number }}</td> <td class="text-center"> <a class="m-r-15 text-muted studentEdits" data-toggle="modal" data-idUpdate="{{ $item->id_student }}" data-target="#userUpdate"> <i class="icofont icofont-ui-edit" style="color: #2196f3;"></i> </a> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> <script> $(document).on('click', '.studentEdits', function() { var _this = $(this).closest('tr'); $('#idUpdate').val($(this).data('idupdate')); $('#e_idStudent').val(_this.find('.idStudent').text()); $('#e_name').val(_this.find('.name').text()); $('#e_sex').val(_this.find('.sex').text()); $('#e_age').val(_this.find('.age').text()); $('#e_email').val(_this.find('.email').text()); $('#e_phone').val(_this.find('.phone').text()); }); </script> @endsection


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