Help us understand the problem. What is going on with this article?

LumenでEloquent

More than 1 year has passed since last update.

Laravelより軽量なLumenということでEloquent使おうとしたらハマった。

いつも通りModelを作って

UserController.php
$user = User::find(1);

実行してみると

FatalThrowableError
Call to a member function connection() on null

んん?
database.phpも大丈夫、というかphp artisan migrateできてる。

調べてみるとLumenではデフォルト設定ではEloquentは無効になってました。
公式ドキュメントにもちゃんと書いてありました。

Eloquent ORM
If you would like to use the Eloquent ORM,
you should uncomment the $app->withEloquent() 
call in your bootstrap/app.php file.

ということで

bootstrap/app.php
$app->withEloquent();

こんな感じにコメントアウトを外したら使えるようになります。
DB::select("SELECT * FROM users");みたいにクエリビルダーを使いたいなら

bootstrap/app.php
$app->withFacades();

こちらもコメントアウト外す必要があります。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした