(1/1) Error

Call to a member function connection() on null
in Model.php line 1201
at Model::resolveConnection(null)in Model.php line 1167
at Model->getConnection()in Model.php line 997
at Model->newBaseQueryBuilder()in Model.php line 912
at Model->newModelQuery()in Model.php line 950
at Model->newQueryWithoutScopes()in Model.php line 901
at Model->newQuery()in Model.php line 1570
at Model->__call('findOrFail', array('2'))in Model.php line 1582
at Model::__callStatic('findOrFail', array('2'))in UserController.php line 15
at UserController->show('2')
at call_user_func_array(array(object(UserController), 'show'), array('2'))in BoundMethod.php line 29
at BoundMethod::Illuminate\Container\{closure}()in BoundMethod.php line 87
at BoundMethod::callBoundMethod(object(Application), array(object(UserController), 'show'), object(Closure))in BoundMethod.php line 31
at BoundMethod::call(object(Application), array(object(UserController), 'show'), array('id' => '2'), null)in Container.php line 564
at Container->call(array(object(UserController), 'show'), array('id' => '2'))in RoutesRequests.php line 373
at Application->callControllerCallable(array(object(UserController), 'show'), array('id' => '2'))in RoutesRequests.php line 339
at Application->callLumenController(object(UserController), 'show', array(1, array('uses' => 'App\\Http\\Controllers\\UserController@show'), array('id' => '2')))in RoutesRequests.php line 313
at Application->callControllerAction(array(1, array('uses' => 'App\\Http\\Controllers\\UserController@show'), array('id' => '2')))in RoutesRequests.php line 275
at Application->callActionOnArrayBasedRoute(array(1, array('uses' => 'App\\Http\\Controllers\\UserController@show'), array('id' => '2')))in RoutesRequests.php line 260
at Application->handleFoundRoute(array(1, array('uses' => 'App\\Http\\Controllers\\UserController@show'), array('id' => '2')))in RoutesRequests.php line 230
at Application->handleDispatcherResponse(array(1, array('uses' => 'App\\Http\\Controllers\\UserController@show'), array('id' => '2')))in RoutesRequests.php line 164
at Application->Laravel\Lumen\Concerns\{closure}()in RoutesRequests.php line 413
at Application->sendThroughPipeline(array(), object(Closure))in RoutesRequests.php line 166
at Application->dispatch(null)in RoutesRequests.php line 107
at Application->run()in index.php line 31
* .\routes\web.php

$router->get('user/{id}', 'UserController@show');
* .\app\Controllers\UserController.php

<?php
namespace App\Http\Controllers;
use App\User;

class UserController extends Controller {

public function show($id) {
return User::findOrFail($id);
}

public function showProfile() {
return 'UserController@showProfile';
}

}
* .\app\User.php

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable;

protected $table = 'users';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email',
];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password',
];

public $timestamps = true;
}
* migration 创建表迁移文件

php artisan make:migration users
 

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci';

$table->increments('id');
$table->string('name', 128);
$table->string('email', 255);
$table->string('password', 32);
$table->timestamps();
$table->unique('email');
$table->index('name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
* seeder 填充数据

php artisan make:seeder UsersTableSeeder

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
$users = DB::table('users');
for ($i = 0; $i < 50; $i++) {
$users->insert([
'name' => str_random(10),
'email' => str_random(10).'@qq.com',
'password' => crypt('secret', 'salt'),
'created_at' => date('Y-m-d H:i:s', time()),
'updated_at' => date('Y-m-d H:i:s', time())
]);
}
}
}
# 执行填充

php artisan db:seed --class=UsersTableSeeder

 

* 浏览器访问 http://localhost:8000/user/2

php -S 0.0.0.0:8000 -t public
* 解决方法:

 .\bootstrap\app.php

$app->withEloquent();
lumen框架中这行默认被注释了