【问题标题】:Uncaught Error: Call to a member function connection() on null in php未捕获的错误:在 php 中调用 null 上的成员函数 connection()
【发布时间】:2020-04-20 20:27:48
【问题描述】:

我尝试在我的项目中使用 Eloquent 而不使用 Laravel,并且 我收到此错误

未捕获的错误:在 null 上调用成员函数 connection()

当我尝试调用一些 Eloquent 方法时。

例如我的 createTask 函数中的 create 方法

<?php

class HomeController extends Controller{

    protected $task;

    public function __construct(){
        $this->task = $this->setModel("Task");
    }

    public function index($name = ""){
        $task = $this->task;
        $task->username = $name;

        $this->setView("home/index", ["name" => $task->username, "email" => $task->email] );

    }

    public function createTask($username = "", $email = "", $task = "")
    {
        $this->task->create([
            "username" => $username,
            "email" => $email,
            "task" => $task
        ]);
    }

}

?>

路由效果很好,所以这里是模型

<?php

use Illuminate\Database\Eloquent\Model as Eloquent;

class Task extends Eloquent
{
    protected $fillable = ["username", "email", "task"];
}

?>

我做错了什么?所有的连接值也是正确的

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver' => 'mysql',
    'host' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'todomvc',
    'charset' => "utf8",
    'collation' => 'utf8_unicode_ci',
    'prefix' => ''
]);

$capsule->bootEloquent();

?>

【问题讨论】:

    标签: php laravel eloquent illuminate-container


    【解决方案1】:

    您在命名空间中输入了Capluse 而不是Capsule,这是一个错误

    use Illuminate\Database\Capluse\Manager as Capsule;
    

    【讨论】:

    • 天哪,这太假了,谢谢!还是不行:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2019-02-24
    相关资源
    最近更新 更多