2016-05-12 71 views
1

我使用laravel 5.2的流明并编辑.env文件中的app_key和数据库信息,也取消注释$ app-> withFacades();在bootstrap/app.php中,现在我可以连接到我的数据库。
问题是我想在我的项目中使用模型,但总是失败。 在app /模型我的模型店/ user.php的在laravel上创建模型5.2

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 

我曾尝试

  • 编辑为database.php流明的框架/ config/
  • 复制并将database.php放在app/Config/

仍然无法正常工作。有没有我想念的东西?

+1

您是否在'bootstrap/app.php'文件中取消了'$ app-> withEloquent();'的注释? – camelCase

+1

我没有注意到..现在它的工作.. –

+0

容易错过,之前也做过。我为未来的读者添加了答案。 – camelCase

回答

0

你很近,只需要在你的bootstrap/app.php文件中取消注释$app->withEloquent();!这将允许你在流明中使用雄辩。从docs

如果你想用雄辩的ORM,您应该取消注释您bootstrap/app.php文件 $app->withEloquent()电话。