6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LumenでEloquent

Posted at

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();

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

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?