问题描述
在null上调用成员函数connection()是尝试在流明中使用Eloquent模型时收到的错误.
Call to a member function connection() on null is the error I'm receiving when trying to use an Eloquent Model in Lumen.
控制器功能:
/**
* 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;
}
型号:
<?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'
];
}
我对Laravel很有经验,但是刚开始我的第一个Lumen项目仅用于API,我不确定为什么会引发此错误.也许这只是我的连接设置?所有查询都必须以以下方式运行吗:
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?:
$results = app('db')->select("SELECT * FROM users");
谢谢!
推荐答案
您应该在bootstrap/app.php
中取消注释雄辩的$app->withEloquent()
调用.
You should uncomment the Eloquent $app->withEloquent()
call in bootstrap/app.php
.
https://lumen.laravel.com/docs/5.2/database#基本用法
更新:
最新版本的文档 https://lumen.laravel.com/docs/5.8/database ,请选中口才ORM
Latest version of the docs https://lumen.laravel.com/docs/5.8/database, check section Eloquent ORM
这篇关于PHP Lumen在null上调用成员函数connection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!