【发布时间】:2014-12-25 10:30:40
【问题描述】:
我想使用 Illuminate 数据库(https://github.com/illuminate/database)。不适用于 Laravel,仅在我的 php 文件中使用。 我愿意
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' => '',
]);
但它似乎不起作用,并且不显示任何错误消息。我需要任何文件吗?照明目录与我的 php 文件在同一目录中。
编辑:
我现在可以使用查询了。像这样
$users = Capsule::table('users')->where('votes', '>', 100)->get();
我不知道如何使用模型。
用户.php
class User extends Illuminate\Database\Eloquent\Model {
}
我的 php 文件
require 'vendor/autoload.php';
require 'User.php';
$users = User::where('status', '=', 1)->get();
有错误
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
已解决:
一切正常。使用@majid8911 示例https://github.com/mattstauffer/IlluminateNonLaravel 谢谢大家。
【问题讨论】:
-
你执行作曲家更新了吗?
-
尝试添加
$capsule->setAsGlobal();
标签: php laravel illuminate-container