Scottie Joe
Scottie Joe

Reputation: 97

Setting SQLite as my database in laravel 5

I'm creating a small mockup for a social network in Laravel 5. I want to use SQLite as my database to keep things small and local. I'm having some trouble, however, getting it to work.

Here's my code (using blade) where I just use a table to display a row in the database:

@extends('layouts.master')

@section('title')
    Testing2
@stop

@section('content')
This is a second test
<a href="/">Back to page 1</a>

<table>
    <tr><th>Author</th>
        <th>Text</th></tr>
    @foreach($posts as $post)
        <tr>
            <td>
                {{$posts->Author}}
            </td>
        </tr>
        <tr>
            <td>
                {{$posts->Text}}
            </td>
        </tr>
    @endforeach
</table>
@stop

"Author" and "Text" being 2 columns in my database. Here is the Route I use to generate the page:

Route::get('test2', function () {
    $sql = "select * from Posts";
    $posts = DB::select($sql);
    return View::make('test2')->withPosts($posts);
});

I know my database is there, I've placed it in the /database directory of my app:

enter image description here

Lastly, I modified the config\database.php file to set SQLite as the default database:

enter image description here

When I try and view the blade page using the Route function, I get the following error: "SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it."

I'm doing something wrong somewhere with connecting to my database, but I don't know what. Have I set it up properly?

Upvotes: 0

Views: 1058

Answers (2)

James
James

Reputation: 16339

You have set your DB_CONNECTION inside your .env file as mysql.

The line 'default' => env('DB_CONNECTION', 'sqlite'), basically says look in my .env file first; if there is a setting set for DB_CONNECTION then use that, if there isn't then use sqlite.

It's an easy fix, just change the DB_CONNECTION config value to sqlite instead of mysql.

Upvotes: 0

Piya Desai
Piya Desai

Reputation: 130

Place your database in the storage folder and mention the path in the database. Also you should have the php driver installed.

Do this in the app/config/database.php:

<?php
return array(
'default' => 'sqlite',
'connections' => array(
    'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),
),
);
?>

Upvotes: 1

Related Questions