PHP Lumen 在 null 上调用成员函数 connection()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37350404/
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
PHP Lumen Call to a member function connection() on null
提问by bi4nchi
Call to a member function connection() on null is the error I'm receiving when trying to use an Eloquent Model in Lumen.
在 null 上调用成员函数 connection() 是我在 Lumen 中尝试使用 Eloquent 模型时收到的错误。
Controller func:
控制器功能:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$employees = Employee::orderBy('first_name', 'asc')->get();
dd($employees);
$response['precontent'] = view('admin::employee.search')->render();
$response['content'] = view('admin::employee.index')
->with(['employees' => $employees])
->render();
$response['title'] = 'Employees';
return $response;
}
Model:
模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
protected $table = 'system_core.employees';
protected $fillable = [
'user_id',
'first_name',
'last_name',
'position',
'primary_address',
'secondary_address',
'phone_1',
'phone_2',
'birth_date',
'start_date',
'end_date'
];
}
I'm pretty experienced with Laravel, but just started my first Lumen project for mere API use and I'm not sure why this error is being thrown. Perhaps it's just my connection settings? Would all queries have to be run the following way?:
我对 Laravel 非常有经验,但刚刚开始我的第一个 Lumen 项目只是为了 API 使用,我不确定为什么会抛出这个错误。也许这只是我的连接设置?所有查询都必须按以下方式运行吗?:
$results = app('db')->select("SELECT * FROM users");
Thank you!
谢谢!
回答by thefallen
You should uncomment the Eloquent $app->withEloquent()
call in bootstrap/app.php
.
您应该取消注释中的 Eloquent$app->withEloquent()
调用bootstrap/app.php
。
https://lumen.laravel.com/docs/5.2/database#basic-usage
https://lumen.laravel.com/docs/5.2/database#basic-usage
Update:
更新:
Latest version of the docs https://lumen.laravel.com/docs/5.8/database, check section Eloquent ORM
最新版本的文档https://lumen.laravel.com/docs/5.8/database,检查Eloquent ORM部分
回答by Valentine Shi
As per 2020 here is the check list to check against to fix this error.
根据 2020 年,这里是用于检查以修复此错误的检查表。
You have to:
你必须:
- Create the database manually;
- Configurethe database connection it in the
.env
file (i.e. setDB_CONNECTION
,DB_DATABASE
,DB_USERNAME
,DB_PASSWORD
); - As per above answers uncomment
$app->withFacades();
,$app->withEloquent();
lines inbootstrap/app.php
; - If you use your Eloquent model within PHPUnit tests you have to boot the Lumen(or Laravel) first by adding the following line to your test class
setUp()
method:
- 手动创建数据库;
- 在
.env
文件中配置它的数据库连接(即 setDB_CONNECTION
,DB_DATABASE
,DB_USERNAME
,DB_PASSWORD
); - 按照上面的答案取消注释
$app->withFacades();
,$app->withEloquent();
输入bootstrap/app.php
; - 如果您在 PHPUnit 测试中使用 Eloquent 模型,您必须首先通过将以下行添加到您的测试类方法来启动 Lumen(或 Laravel):
setUp()
parent::setUp()
That should fix it.
那应该解决它。