Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRobert Young
11,742 PointsHow do I change the database that laravel connects to in the laravel configuration settings?
Hi there,
In the tutorial, Hampton has instructed us to create a new database called Odot. I have created the database, and I want to change the configuration settings inside my Laravel code to connect to this database.
I am using a new version of Laravel 5. Firstly I cannot seem to find a folder in laravel-basics/app/config called 'local' and containing a second 'database.php' file. There only seems to be one database.php file, and that is in the top level of laravel-basics/app/config.
Then when I change the database name in that database.php file under the mysql object to env('DB_DATABASE', 'odot'), nothing happens, when I run 'return DB::select('select database();')' inside my routes.php file I still get '[{"database()":"homestead"}]'.
I would be grateful for some assistance, perhaps I'm missing out something?
Some more or my code is below:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path().'/database.sqlite',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'odot'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false"
<?php
Route::get('/', 'TodoListController@index');
Route::get('/db', function(){
return DB::select('select database();');
});
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use View;
class TodoListController extends Controller { // extends the controller from Laravel
public function index()
{
//return View::make('todos.show')->withId($id);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
public function store()
{
//
}
public function show($id)
{
return View::make('todos.show')->withId($id);
}
Thanks!
Robert
London, UK
1 Answer
Greg Kaleka
39,021 PointsHi Robert,
In Laravel 5.x, there is not a separate local folder. Instead, you define your environment in a file in the root of your project called .env.example
. From what I've read, this is a much cleaner way of dealing with separate environments compared to how it was handled in 4.x.
Anyway, all you have left to do to change the database being used is go into .env.example
and change DB_DATABASE=homestead to DB_DATABASE=odot. You'll then want to make the same change in .env
. I'm actually not clear on the purpose of both of these files... maybe someone else can chime in??
APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
DB_HOST=localhost
DB_DATABASE=odot
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
Robert Young
11,742 PointsRobert Young
11,742 PointsThanks Greg - this seemed to work for me! Thanks for clearing up this mysterious matter - I'd have never have sussed it otherwise!
Cheers!
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsHappy to help! Frankly, this course has been somewhat of a headache for me. I'm probably ~10 lessons ahead of you... it doesn't get less frustrating, I'm afraid!
I really hope this gets updated now that Laravel is up to version 5.1 LTS (long term support), and won't change majorly for the next couple of years.
Also - I edited your code to add code highlighting. Click the ... button below your post and choose edit to see the changes I made for future reference!
Mark Zeitler
6,718 PointsMark Zeitler
6,718 PointsGreg Kaleka The difference between the .env and .env.example files is actually quite simple. The .env file is meant only for the environment that the project is being run on (e.g. Individual developers may want different environment settings than other developers working on the project), whereas the .env.example file is meant to be an example for all other environments (e.g. A new developer pulling for the first time) so that the environment can get up and running easily. Typically, you don't want the .env file to be in any git repository if you use one, because one developer's .env might be different from another's. You would, however, want to have the .env.example file in git for new environments so that all you need to do is copy the .env.example to a new .env file and then customize as necessary.
Sorry if that wasn't clear. Tried my best to make that as simple as possible.
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsHeh thanks Mark. I've figured this out in the ensuing year, but it's good to have this info here if other students find this old post.