简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

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?

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'   => '',
    ),
),
);
?>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM