我正在使用 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/
仍然无法正常工作。有什么我想念的吗?