PHP Lumen Call to a member function connection() on null

大憨熊 提交于 2019-12-12 09:32:57

问题


Call to a member function connection() on null is the error I'm receiving when trying to use an Eloquent Model in Lumen.

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?:

$results = app('db')->select("SELECT * FROM users");

Thank you!


回答1:


You should uncomment the Eloquent $app->withEloquent() call in bootstrap/app.php.

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://stackoverflow.com/questions/37350404/php-lumen-call-to-a-member-function-connection-on-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!