Search code examples
phplaravelilluminate-container

Use Illuminate database


I want to use Illuminate database(https://github.com/illuminate/database). Not with Laravel, use only in my php file. I do

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

But it seems not working, and don't show any error message. Do I need to require any file? The illuminate directory is in the same directory with my php file.

EDIT:

I can use query now. Like this

$users = Capsule::table('users')->where('votes', '>', 100)->get();

I don't know how to use model.

User.php

class User extends Illuminate\Database\Eloquent\Model {
}

My php file

require 'vendor/autoload.php';
require 'User.php';
$users = User::where('status', '=', 1)->get();

Got error

Fatal error: Call to a member function connection() on a non-object in /Users/someone/repos/test/vendor/illuminate/database/Illuminate/Database/Eloquent/Model.php on line 2472

SOLVED:

Everything works fine. Use @majid8911 example https://github.com/mattstauffer/IlluminateNonLaravel Thank you everyone.


Solution

  • take a look at here I successfully did the same with this tutorial: https://github.com/mattstauffer/IlluminateNonLaravel