DominiqueGEORGES's avatar

Use already existing database in eloquent

Hi,

I have a SQLite database on which I need to build reports, ...

I try to use DB('mySQLiteDB')->select('SELECT * FROM table1')->paginate(5);

I do that to create readable Web pages.

Problem, it seems, it requires the database to be defined in Eloquent.

I just passed 4 hours on Google to try to discover something easy to understand (Eloquent for dummies) but .. nothing is there.

The Laravel documentation about Eloquent is just unreadable for dummies !

I'm an old developer (who prefers having everything under control), like in Perl or simple PHP/Python.

I tried to use the most easiest framework to build my web site, but, except a violent headache, nothing usefull, nothing trivial, nothing easy to understand and manage.

I need your help :)

My simply first page is just displaying SQL select result in a paginated table (adding CRUD functionalities).

Nothing more.

Thanks in adavance.

Kr, Dom

0 likes
13 replies
automica's avatar

@dominiquegeorges you have to define the database first but that's not difficult. Add yours to 'connection' settings in

config/database.php

Then you can use the new connection in query builder.

DB::connection('mysql2')->table('node')->where('type', 'Programs')->get();

if you are new to Laravel, then learning how eloquent works is essential.

Check out basics here: https://laracasts.com/series/eloquent-relationships

I don't think the Laravel documentation is too hard to grasp, but as an old coder (46 next birthday) I do have other MVC experience.

Go through this series first (available if you aren't a subscriber too)

https://laracasts.com/series/laravel-6-from-scratch

1 like
DominiqueGEORGES's avatar

Hi automica :)

Thanks for your reply.

My database.php already contains :

'connections' => [

    'CER' => [
        'driver' => 'sqlite',
        'database' => 'C:/Tools/myDatabase.sqlite',
        'prefix' => '',   
    ],
    

In the controller, my SELECT works perfectly :

$recorders = \DB::connection('CER')->selectRaw("SELECT * FROM recorders");

But, if I add the "paginate(5)

$recorders = \DB::connection('CER')->selectRaw("SELECT * FROM recorders")->paginate(5);

 it fails
DominiqueGEORGES's avatar

:o

It was so .. easy ? I can't believe it !

Documentation is making so complex what so easy is !

MANY THANKS :)

In 2 minutes you made my day :)

Take care about you.

Kind regards, Dom

automica's avatar

@dominiquegeorges happy to help. be sure to check out the series. The annual subscription has been the best money I've ever spent in development. Second best was buying phpstorm license.

2 likes
rodrigo.pedra's avatar

Did you watch the

http://laravelfromscratch.com/

Series already? It is a free series covering most of Laravel's key concepts, including Eloquent. Although this series covers Laravel version 6, core concepts didn't change from that version.

Also this code:

DB('mySQLiteDB')->select('SELECT * FROM table1')->paginate(5);

Doesn't seem to be a Laravel code. I am not aware of any DB(...) helper function.

You are maybe making a confusion with the DB Facade, apologize me if that is not the case.

Using that Façade you could use:

// add this import on the top of your file file
use \Illuminate\Support\Facades\DB;

$rows = DB::table('table1')->paginate(5);

For that to work you will need to specify you database connection parameters. For SQLite add this to your .env file:

#################################
# COMMENT OUT standard DB config
#################################
#DB_CONNECTION=mysql
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=laravel
#DB_USERNAME=root
#DB_PASSWORD=

#################################
# Add this DB config
#################################
DB_CONNECTION=sqlite
DB_DATABASE=/path/to/mySQLiteDB.sqlite

Using Eloquent is not much different.

You will need a model class, you can make one in your CLI using this command:

php artisan make:model MyModel

The file ./app/Models/MyModel.php should be created.

Open this new file and add a $table property:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
    use HasFactory;
    
    // This is optional, but when not informed
    // Eloquent will try to guess the table's name
    // by convention it will try to pluralize
    // the class name, in this case it would
    // look for a table named: "my_models"
    protected $table = 'table1';
}

The in you controller:

use \App\Models\MyModel;

$rows = MyModel::paginate(5);

Hope it helps.

DominiqueGEORGES's avatar

Arg,

indeed, it is more "facade" than "eloquent".

I just need to use my DB and work on it :)

Creating the modem means I have to describe all the tables, fields, keys, .. ?

Kr. Dom

rodrigo.pedra's avatar

No, if you use Laravel conventions for guessing table names, and have some default fields (primary key called id, and created_at and updated_at timestamps) you can get away with very minimal configuration on your models.

Spent some time in the series both I and @automica referenced (mine is a link owned by Laracasts that redirects to the same series), and you will be up and running in no time.

1 like
DominiqueGEORGES's avatar

"All in one" tools like Laravel are powerfull, but sometime it takes more time to configure the tool rather than using it :(

Reason why I love Perl, basic PHP ou Python, even PowerShell (I'm a Windows boy ;) )

DominiqueGEORGES's avatar

I'm using Visual Studio Code ;)

I'll have a look at the annual subscription also.

Will the CRUD functionality be as easy ?

Kr Dom

rodrigo.pedra's avatar

Was typing my answer and didn't see @automica already helped you out.

And in a better, more concise and more helpful way =)

Hope all this helps you advancing on Laravel.

2 likes
DominiqueGEORGES's avatar

But your explanation was also VERY INTERRESTING ;)

Thanks for taking time to help me.

I really appreciate

Kr, Dom

2 likes

Please or to participate in this conversation.