How to use DB Laravel ?

Updated at 22-Aug-2022, By samar

How to use DB Laravel ?

In this session, we are going to try to solve the "How to use DB Laravel ?" puzzle by using the computer language.

You can use `DB::select('select * from users');` query after importing the `use Illuminate\Support\Facades\DB;` in class after namespace.
  • app\Http\Controllers\<HomeController>.php

    Import DB facade after namespace (namespace App\Http\Controllers;) and before class definition (class HomeController extends Controller) in HomeController class.

    use Illuminate\Support\Facades\DB;
    

    Code in controller method

    $users = DB::select('select * from users');
    return view('home', compact('users'));
    

    resources\views\home.blade.php

    @foreach ($users as $user)
        <p>This is user {{ $user->id }}</p>
    @endforeach
    

Back to code snippet queries related laravel