未捕获错误:在php中对null调用成员函数connection()



我尝试在项目中使用Eloquent而不使用Laravel我收到这个错误

未捕获错误:在空上调用成员函数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 IlluminateDatabaseEloquentModel as Eloquent;
class Task extends Eloquent
{
protected $fillable = ["username", "email", "task"];
}
?>

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

<?php
use IlluminateDatabaseCapsuleManager 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();
?>

您在名称空间中键入Capluse而不是Capsule时出错

use IlluminateDatabaseCapluseManager as Capsule;

最新更新