简体   繁体   中英

create model on laravel 5.2

I'm using lumen from laravel 5.2 and edit app_key and database info in .env file also uncomment $app->withFacades(); in bootstrap/app.php so now i can connect to my database.
the problem is i want to use model in my project but always failed. my model store in app/Models/User.php

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

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

my controller

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;
    }
}

i also tried change

use App\Models\User;

with

use App\User;  

but still not working.

here my error message

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

in my xampp server there is also this message

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

what i have tried

  • edit database.php in lumen-framework/config/
  • copy and put database.php in app/Config/

and still not working. is there something i miss?

You're close, just need to uncomment $app->withEloquent(); in your bootstrap/app.php file as well! This will allow you to use Eloquent within Lumen. From the docs :

If you would like to use the Eloquent ORM, you should uncomment the $app->withEloquent() call in your bootstrap/app.php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM