1

This might be silly question, but since I am new to laravel, I am finding it somewhat difficult to setup database. I am using laravel 5.2. IN config>database.php these are my settings

'default' => env('DB_CONNECTION', 'mysql'),

and for mysql :

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'larasite'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

Then I also changed .env and .env.example file's settings

DB_HOST=localhost
DB_DATABASE=larasite
DB_USERNAME=root
DB_PASSWORD=""

Now when in routes.php I have this code:

Route::get('about', function(){
  $posting = DB::query('select * from posts');
  dd($posting);
});

And then when I visit localhost:8000/about I get following error.

Builder {#118 ▼
#connection: MySqlConnection {#114 ▶}
#grammar: MySqlGrammar {#115 ▶}
#processor: MySqlProcessor {#116}
#bindings: array:6 [▶]
+aggregate: null
+columns: null
+distinct: false
+from: null
+joins: null
+wheres: null
+groups: null
+havings: null
+orders: null
+limit: null
+offset: null
+unions: null
+unionLimit: null
+unionOffset: null
+unionOrders: null
+lock: null
#backups: []
#bindingBackups: []
#operators: array:26 [▶]
#useWritePdo: false
}

I noticed that, even If I change settings and enter wrong details, I get same error. What's happening? How to solve this?

3
  • Why did you change the mysql portion of the code? Laravel is set up so that you only have to edit the .env Commented Jan 29, 2016 at 19:22
  • @OliverQueen is that it? But I read some tutorials that said change the database file. May be it was for older version. I triied undoing all changes in database.php file and it's still the same! Commented Jan 29, 2016 at 19:26
  • Also, you should probably be executing your query in a controller with the DB namespace added to it Commented Jan 29, 2016 at 19:26

1 Answer 1

2

That's not an error, that's the dump of the Query Builder instance you're assigning to the $posting variable. You must use the get method to actually fetch the data from the database and also you should be using just the simple table method, because the rest of the query will be generated by the Query Builder.

The code below will return all entries in the posts table:

Route::get('about', function(){
  $posting = DB::table('posts')->get();
  dd($posting);
});

You should read the Query Builder Documentation from start to finish to get a better understanding of how it all works. It's way easier that just reading bits and pieces of it and it's not a very long read.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks this worked. I will read the latest documentation. BTW why do you think raw query does not work? yes this table() works, but ->get() does not work with previous raw query
You're very welcome. The query method just creates an instance of the query builder (the string query you passed as an argument doesn't do anything), if you want to run raw SQL queries you should be using DB::select('select * from posts') (without get because this is defined on the database Connection class). Or for the query approach to work you would need to have DB::query()->from('users')->get();. It might sound a little complicated at first but once you get more experienced with the framework it will become easy. For starters just follow the documentation and you'll be fine :).

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.