laravel 使用 Illuminate 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27646396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:35:39  来源:igfitidea点击:

Use Illuminate database

phplaravelilluminate-container

提问by Howard

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

我想使用 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'    => '',
]);

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.

但它似乎不起作用,并且不显示任何错误消息。我需要任何文件吗?照明目录与我的php文件在同一目录中。

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

用户名.php

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

My php file

我的php文件

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/IlluminateNonLaravelThank you everyone.

一切正常。使用@majid8911 示例https://github.com/mattstauffer/IlluminateNonLaravel谢谢大家。

回答by mhndev

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

看看这里我成功地对本教程做了同样的事情:https: //github.com/mattstauffer/IlluminateNonLaravel