How to use DB Laravel ?

Created 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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.