Querying using raw SQL statements
Laravel provides many ways to access our database. If we have existing queries that we've used before, or if we need something a bit more complicated, we can use raw SQL to access our database.
Getting ready
For this recipe, we'll be using the table created in the Creating data tables using migrations and schema recipe.
How to do it...
To complete this recipe, follow these steps:
In the command prompt, create a migration so we can add some data:
php artisan migrate:make add_data_to_shows_table
In our
app/database/migrations
directory, find a file similar to2012_01_01_222551_add_data_to_shows_table.php
, and add some data using raw SQL:class AddDataToShowsTable { /** * Make changes to the database. * * @return void */ public function up() { $sql = 'INSERT INTO shows (name, rating, actor) VALUES (?, ?, ?)'; $data1 = array('Doctor Who', '9', 'Matt Smith'); $data2 = array('Arrested Development...