1

我正在使用 laravel 5.2 中的 lumen 并在 .env 文件中编辑 app_key 和数据库信息,同时取消注释 $app->withFacades(); 在 bootstrap/app.php 中,所以现在我可以连接到我的数据库。
问题是我想在我的项目中使用模型但总是失败。我的模型存储在 app/Models/User.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
  protected $table = 'user';
  protected $fillable = ['userid','name','timestamp'];
}

我的控制器

namespace App\Http\Controllers;
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');

use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use DB;

use App\Models\User;

class Controller extends BaseController
{
    public function tes(Request $request){
        $user = User::where('id','=',1)->first();

        return 'name: '.$user->name;
    }
}

我也试过改变

use App\Models\User;

use App\User;  

但仍然无法正常工作。

这是我的错误信息

FatalErrorException in Model.php line 3280:
Call to a member function connection() on null  

在我的 xampp 服务器中也有此消息

Fatal error: Call to a member function connection() on null in D:\Workspace\website\api\vendor\illuminate\database\Eloquent\Model.php on line 3280  

我试过的

  • 在 lumen-framework/config/ 中编辑 database.php
  • 复制并将 database.php 放入 app/Config/

仍然无法正常工作。有什么我想念的吗?

4

1 回答 1

0

你很接近,只需要$app->withEloquent();在你的bootstrap/app.php文件中取消注释!这将允许您在 Lumen 中使用 Eloquent。从文档

如果你想使用 Eloquent ORM,你应该 在你的文件中取消注释$app->withEloquent()调用。bootstrap/app.php

于 2016-05-12T16:12:55.937 回答