0

I'm using Laravel and trying to do an SQL query from my Controller in a public function, but I'm really confused where I would put my table in the argument and if quotes go around the argument. Here is my code

 public function selectMethod(){
    $results = DB::select('select firstname from people where id = 1');
    print_r($results);
    return view('pages.selectMethod');
}

table is called people

My .env is configured to my database correctly and I get this error

FatalErrorException in AboutController.php line 90: Class 'App\Http\Controllers\DB' not found

Thanks !

2
  • Only add ----use DB;---- namespace Commented Jul 9, 2017 at 5:11
  • use \DB instead of DB Commented Jul 9, 2017 at 5:59

2 Answers 2

1

you should add use Illuminate\Support\Facades\DB; at the top of your page for example :

    <?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.index', ['users' => $users]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your error clearly states: Class 'App\Http\Controllers\DB' not found

Hence just use DB in your class. Add:

use DB;

At the top of the file just below the namespace line.

Also, I would suggest you to use Eloquent for your queries. It will make your life a lot easier and your code a lot beautiful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.