46

I'm receiving this error when trying to use an Eloquent Model in Lumen.

Call to a member function connection() on null

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!

francisco
  • 1,387
  • 2
  • 12
  • 23
bi4nchi
  • 519
  • 1
  • 4
  • 7

7 Answers7

145

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

Kazmi
  • 1,198
  • 9
  • 20
thefallen
  • 9,496
  • 2
  • 34
  • 49
19

You have to:

  1. Create the database manually via e.g. PHPMyAdmin;
  2. Configure the database connection it in the .env file (i.e. set DB_CONNECTION, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
  3. As per above answers uncomment $app->withFacades();, $app->withEloquent(); lines in bootstrap/app.php;
  4. 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:
    parent::setUp()
    
miken32
  • 42,008
  • 16
  • 111
  • 154
Valentine Shi
  • 6,604
  • 4
  • 46
  • 46
9

Go to bootstrap/app.php

then uncomment

$app->withEloquent();
Feisal Ali
  • 351
  • 2
  • 9
3

I think you just uncomment $app->withFacades();, $app->withEloquent(); lines in bootstrap/app.php;

And check again, it works for me.

Ankur prajapati
  • 485
  • 6
  • 9
2

Another less common reason for this error is that you are trying to access an eloquent model before it is ready, e.g in the constructor of a Job or perhaps in app/Console/Kernel.php

If this is the case, try doing this instead

use Illuminate\Support\Facades\DB;

$model =  DB::table('table_name')
            ->where('field_name', 'field_value')->first()
Otobong Jerome
  • 401
  • 6
  • 5
0

I've tried different solutions but seems like problem was very simple.

composer du

works for me.

Afzal Ali
  • 880
  • 8
  • 25
-1

Go to bootstrap/app.php

then uncomment

$app->withEloquent();

worked for me!

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 02 '23 at 18:45