11.08.2017 Views

codebright

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Databases 209<br />

As you can see, you have a great deal of choice when selecting a database platform. For this book,<br />

I will be using the MySQL Community Server Edition²⁵. It’s a great free platform, and one of the<br />

most popular ones used for development.<br />

You don’t have to worry about using another database server though. You see, Laravel provides an<br />

abstraction layer, it decouples the framework’s database components from the RAW SQL, providing<br />

different queries for different types of databases. Simply put, you don’t have to worry about the SQL<br />

syntax, let Laravel take care of it.<br />

Another advantage of using Laravel’s database abstraction layer is security. In most situations, unless<br />

I indicate otherwise, you won’t have to worry about escaping the values that you send to the database<br />

from Laravel. Laravel will escape these values for you, in an effort to prevent various forms of<br />

injection attacks.<br />

Let’s weigh up the flexibility of Laravel’s database abstraction layer for a moment. So you can switch<br />

database servers whenever you like, without having to change any of the database code you have<br />

written, and you won’t have to worry about simple matters of security? That to me sounds like a<br />

great chunk of work has been removed from your projects. Escaping values is boilerplate, we don’t<br />

need to do that. Let’s let Laravel take care of it.<br />

Now that we know that we wish to use a database, let’s learn how we can setup Laravel to use one.<br />

Don’t worry, it’s quite a simple process! First let’s take a look at the configuration options.<br />

Configuration<br />

All of Laravel’s database configuration is contained in the file located at app/config/database.php.<br />

That’s easy to remember, right? Let’s take a trip through the file, and look at some of the<br />

configuration options available.<br />

1 /*<br />

2 |--------------------------------------------------------------------------<br />

3 | PDO Fetch Style<br />

4 |--------------------------------------------------------------------------<br />

5 |<br />

6 | By default, database results will be returned as instances of the PHP<br />

7 | stdClass object; however, you may desire to retrieve records in an<br />

8 | array format for simplicity. Here you can tweak the fetch style.<br />

9 |<br />

10 */<br />

11<br />

12 'fetch' => PDO::FETCH_CLASS,<br />

²⁵http://www.mysql.com/products/community/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!