Running Raw SQL Queries: Running A Select Query

<?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::select('select * from users where active = ?', [1]);

        return view('user.index', ['users' => $users]);
    }
}

Select method

Foreach ($users as $user) {
    echo $user->name;
}

The first argument passed to the select method is the raw SQL query, while the second argument is any parameter bindings that need to be bound to the query.

Will always return an array of results.

Running Raw SQL Queries: Running A Select Query — Structure map

Clickable & Draggable!

Running Raw SQL Queries: Running A Select Query — Related pages: