Laravel 5使用用户输入创建数据库连接

I'm creating a CMS for a small project and need the user to be able to connect to a database during the initial setup.

I'm using a middleware to check whether the database connection is correct (and the tables exist) and if so to run the application. If not, to run the setup.

public function handle($request, Closure $next) {
    if(DB::connection()) {
        return $next($request);
    } else {
        return redirect('/setup');
    }       
}

Essentially, the user will create a database themselves, then supply the database name, username and password during setup which will create a .env file with these details.

However, the application keeps failing with this error:

SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)

Is there a way to make this work? Or is there a better way to go about this?

Thanks

From the mysql error you are not passing the mysql username and password to the query.
Access denied for user 'you should have something here'@'localhost'
using password: NO this should be YES
Since you are getting the mysql user and pass from the user running the setup script.