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/
仍然无法正常工作。有没有我想念的东西?
您是否在'bootstrap/app.php'文件中取消了'$ app-> withEloquent();'的注释? – camelCase
我没有注意到..现在它的工作.. –
容易错过,之前也做过。我为未来的读者添加了答案。 – camelCase