代码之家  ›  专栏  ›  技术社区  ›  gondai yosuke

在laravel 5.2上创建模型

  •  1
  • gondai yosuke  · 技术社区  · 9 年前

    我使用的是laravel 5.2中的流明,并在.env文件中编辑app_key和数据库信息,还取消注释$app->带Facades();在引导程序/应用程序中。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  
    

    我试过的

    • 编辑数据库。流明框架/config中的php/
    • 复制并放置数据库。app/Config中的php/

    但仍然没有工作。有什么我想念的吗?

    1 回复  |  直到 9 年前
        1
  •  0
  •   camelCase    9 年前

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

    如果您想使用Eloquent ORM,您应该取消注释 $app->withEloquent() 打电话给你 引导程序/app.php 文件