(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框架中这行默认被注释了

PHP Lumen Call to a member function connection() on null 报错的更多相关文章

  1. laravel和lumen数据库链接错误_FatalErrorException Call to a member function connection

    FatalErrorException in Model.php line 3339: Call to a member function connection() on null 挺简单的一个lum ...

  2. Call to a member function allowField() on null 错误总结

    Call to a member function allowField() on null 在空对象上调用  allowField() 没有该模型对象无法调用,需要创建相应的模型 错误版本: if ...

  3. Call to a member function assign() on null

    Thinkphp: 在子控制器里面写了一个构造函数,如下 //构造函数 public function __construct(){ echo 1; } 结果页面报错了  ---->  Call ...

  4. Wordpress 数据库查询错误 Call to a member function get_results() on null

    在插件中的一个文件使用如下代码,无法查询 <body> <?php global $wpdb; $sql = ""; $sql = "SELECT * ...

  5. ssh: connect to host 120.79.26.164 port 22: Connection timed out报错问题

    要是使用阿里云服务器,出现这种错误,一般是端口没有打开.需要在阿里云控制台中设置端口后,即可使用ssh连接.

  6. php 写入数据库时Call to a member function bind_param() on a non-object

    <?php $servername = "localhost"; $username = "username"; $password = "pa ...

  7. Thinkphp---------Call to a member function free_result() on a non-object

    1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...

  8. :( Call to a member function Table() on a non-object 错误位置

    :( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form

  9. Fatal error: Call to a member function bind_param() on a non-object in

    今天在练习 mysql是出现错误:   Fatal error: Call to a member function bind_param() on a non-object in 解决步骤: 1. ...

随机推荐

  1. Java面试知识点之数据库篇(一)

    前言:数据库的相关知识,在面试中也经常出现,笔者认为非常有必要对此类知识进行相关总结. 1.索引 索引是对数据库表中一列或多列的值进行排序的结构,是帮助数据库高效获取数据的数据结构. 通俗理解:索引就 ...

  2. 在Linux上搭建VisualSVN Server(svn服务端)

    一.检查是否安装了低版本的SVN #  rpm -qa | grep subversion 如果已安装SVN,则会返回版本信息.这时需要卸载旧版本的SVN. 卸载旧版本SVN # yum remove ...

  3. 大学?做码农?做project师?

        近期看到一个知乎里非常热闹的讨论.当中讨论到科研能力与project能力,我有非常多感想. 想说说大学CS方向的一些东西.     我不是计算机专业的,如今大二本科工科在读.     我接触编 ...

  4. 局域网下Android与scoket通信的实现

    因为最近实验室项目要求实现在局域网下将android app数据发送到winsock中进行保存,所以对此进行了简单学习.pc端因为是另一个同学做的,所以不做说明. 在android端,首先添加权限: ...

  5. Request Body可以反复读取的方法

    org.springframework.web.filter.HiddenHttpMethodFilter:过滤器已经读取流了,因此想频繁读取流的话,自定义Filter必须在org.springfra ...

  6. js同步-异步-回调

    出处:https://blog.csdn.net/u010297791/article/details/71158212(1)上面主要讲了同步和回调执行顺序的问题,接着我就举一个包含同步.异步.回调的 ...

  7. Linux如何查看端口状态

    netstat命令各个参数说明如下: -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的 ...

  8. vi十六进制编辑

    指定行:n 光标行之前或之后的n个字符nl 之后 2l 光标位置两个字符后nh 之前 2h 光标位置两个字符前 光标行之上或之下的n个字符nk 之上 1k 光标位置1个字符之上nj 之下 1j 光标位 ...

  9. GitHub Or Subversion

    上一次转载了介绍GitHub的博文点我,我想对于初学GitHub的同学们还是有不清楚的地方,毕竟有些概念的理解比较费力.我觉得作为一个对于配置库技术已经有一定基础的同学们,要学习GitHub,最快以及 ...

  10. 正确理解CAP定理

    前言 CAP的理解我也看了很多书籍,也看了不少同行的博文,基本每个人的理解都不一样,而布鲁尔教授得定义又太过的简单,没有具体描述和场景案例分析.因此自己参考部分资料梳理了一篇与大家互相分享一下. 标题 ...