How to foreach loop table using | PHP Laravel

How to foreach loop table using | PHP Laravel

Step 1: Install Laravel 7 Project

(Your original comment said Laravel 5.7, but command and structure indicate Laravel 7 — clarified below)

Open your terminal and run:

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

Step 2: Set Up Database Connection

Edit the .env file in your Laravel project:

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 Route

In routes/web.php, add this route:

// ======================== User ======================== // Route::get('user', 'HomeController@usertotal');

Step 4: Controller Logic

In app/Http/Controllers/HomeController.php, define the method:

use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; public function usertotal() { $user = Auth::user(); $userselect = DB::table('users')->get(); return view('password.user', compact('user', 'userselect')); }

Step 5: Create View

Create the view file at:
resources/views/password/user.blade.php

@extends('layouts.master') @section('menu') @include('sidebar.dashboard') <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 User</h4> <span>User 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="/"> <i class="feather icon-home"></i> </a> </li> <li class="breadcrumb-item">User Table</li> <li class="breadcrumb-item">Back Home</li> </ul> </div> </div> </div> </div> <div class="page-body"> <div class="row"> <div class="col-sm-12"> <!-- User Table Start --> <div class="card"> <div class="card-header"> <h5>User</h5> <span>Count total</span> </div> <div class="card-block"> <div class="dt-responsive table-responsive"> <table id="simpletable" class="table table-striped table-bordered nowrap"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Profile User</th> <th>Date Join</th> <th>Update Join</th> </tr> </thead> <tbody> @foreach($userselect as $totaluser) <tr> <td>{{ $totaluser->id }}</td> <td>{{ $totaluser->name }}</td> <td>{{ $totaluser->email }}</td> <td> <img src="{{ asset('files/assets/images/' . $user->avatar) }}" alt="User Profile" width="50"> </td> <td>{{ $totaluser->created_at }}</td> <td>{{ $totaluser->updated_at }}</td> </tr> @endforeach </tbody> <tfoot> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Profile User</th> <th>Date Join</th> <th>Update Join</th> </tr> </tfoot> </table> </div> </div> </div> <!-- User Table End --> </div> </div> </div> </div> </div> </div> </div> @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