In this tutorial learn How to retrieving all records from database and display records with foreach loop in laravel.
You may use the table method on the DB facade to begin a query. The table method returns a fluent query builder instance for the given table.
The get method returns an IIuminate\Support\Collection containing the results where each result is an instance of the PHP stdClass object. You may access each column's value by accessing the column as a property of the object: use the foreach to display the records
You may use the table method on the DB facade to begin a query. The table method returns a fluent query builder instance for the given table.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show a list of all of the application's users.
*
* @return Response
*/
public function index()
{
$users = DB::table('users')->get();
return view('user', ['users' => $users]);
}
}
2. Create a user in views/ user.blade.php
@foreach ($users as $user)
{{$user->name}}
@
endforeach
No comments:
Post a Comment