Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Work with databases and the query builder in Laravel

Monday, November 30th, 2015 by Servage

database-queryWorking with database driven applications in Laravel is super straightforward. This functionality has been included as a core feature of the framework as database handling is almost done in every project. Using your Servage hosting account you can connect to one or multiple MySQL databases from Laravel and create, edit and delete data as you please. This article will outline some of the standard functionality for this.


You first need to configure your Laravel installation for your environment. Note that you can use local configurations to make the database settings different depending on for example local development environment and production web hosting account. Below you find an example configuration.

'mysql' => [
  'read' => [
    'host' => '',
  'write' => [
    'host' => ''
  'driver' => 'mysql',
  'database' => 'database_name',
  'username' => 'your_username',
  'password' => 'your_password',

The default configuration above uses the same database server for reading and writing and only uses the minimum parameters for a MySQL setup. Replacing the placeholders above should be sufficient to get going.

Example usage

The following is a full controller example for a method that gets a full list of all published articles from a database, and returns them to a view. Note how the example uses clear MySQL syntax instead of “smart” model-based queries. This is done to clearly show how to use a direct query.


namespace App\Http\Controllers;

use DB;
use App\Http\Controllers\Controller;

class ArticlesController extends Controller
  * Show a list of all of the articles in the database.
  * @return Response
  public function index()
    $articles = DB::select('select * from articles where published = ?', [1]);
    return view('articles.index', ['articles' => $articles]);


Model-based query

Instead of using the MySQL syntax directly, you can also use smart queries that are model based. The benefit is that it is easier to write, has more functionality, and gets a lot of functionality right from Laravel for smart querying.

// Instead of the DB::select() above, run a model-based query instead
$articles = Article::where('published', 1)->get();

The line above is using the Eloquent model system from Laravel to automatically create a query that resembles the direct MySQL query entered in the first example. As you see, the code becomes much more readable, and is dynamically adaptable due to methods being chainable.

Chainable methods mean that each method is always returning an object instance of the base class, and thereby you can keep adding methods to it. This is smart when dealing with dynamically built queries etc. Have a look at the example below.

// Setup a query object
$query = Articles::select();

// One where-condition
$query->where('published', 1);

// Multiple where-conditions
$query->where('published', 1)->where('author_id', 2;

You can chain these methods as much as you like, and add conditions, limits, sorting, joins etc. This is a very powerful feature of the Eloquent model database handling, and it makes your database operations a lot easier than regular MySQL syntax writing.

Work with databases and the query builder in Laravel, 4.3 out of 5 based on 6 ratings
Categories: Guides & Tutorials

Keywords: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.