Search code examples
phpmysqllaravelphpstorm

PhpStorm MySQL use DB undefined


  • Laravel version 5.5.20
  • IDE: PhpStorm

Here is my code:

<?php

namespace App\Http\Controllers;

use DB;

class TasksController extends Controller
{
    public function index()
    {
        $users = DB::select('select * from users where active = ?', [1]);

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

At line 5 IDE shows "Undefined class DB", and line 11 doesn't work too.

I tried to change line 5 to use Illuminate\Support\Facades\DB; but line 11 still don't work, it says

Method 'select' not found in Illuminate\Support\Facades\DB

I check my config\app.php, aliases already have 'DB' => Illuminate\Support\Facades\DB::class

I change "use DB" to "Illuminate\Support\Facades\DB",it works! BUT the Phpstorm still draw underline error at select statement, and says "mathod select not found in Illuminate\Support\Facades\DB" is there any way to ignore that eror on Phpstorm IDE tool ?


Solution

  • public function index()
    {
        $users = \DB::table('users')->where('active', 1)->get();
    
        return view('tasks.index', ['users' => $users]);
    }
    

    or

    public function index()
    {
        $users = \DB::raw('select * from users where active = ?', [1]);
    
        return view('tasks.index', ['users' => $users]);
    }