-1

我尝试在我的项目中不使用 Laravel 的情况下使用 Eloquent,但出现此错误

未捕获的错误:在 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();

?>
4

1 回答 1

-1

您通过键入Capluse而不是在命名空间中犯了一个错误Capsule

use Illuminate\Database\Capluse\Manager as Capsule;
于 2020-04-20T20:32:23.020 回答