我正在Laravel 4中构建一个包,但在try 访问数据库时出现了一个非对象错误,该数据库似乎是一个正确实例化的对象.以下是设置:

正在讨论的配置和类:

composer.json:

...
"autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ],
        "psr-0": {
            "Vendor\\Chat": "src/vendor/chat/src"
        }  
    }
...

The class:

namespace Vendor\Chat;

use Illuminate\Database\Eloquent\Model as Eloquent;


class ChatHistory extends Eloquent
{
    protected $table = 'chat_history';

    protected $fillable = array('message', 'user_id', 'room_token');

    public function __construct($attributes = array())
    {
        parent::__construct($attributes);
    }

}

The call:

$message = new Message($msg);

$history = new ChatHistory;
$history->create(array(
                 'room_token' => $message->getRoomToken(),
                 'user_id' => $message->getUserId(),
                 'message' => $message->getMessage(),
              ));

The error:

PHP Fatal error:  Call to a member function connection() on a non-object in /home/vagrant/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 2894

我相信我错过了一些基本的东西.谢谢你的帮助!

编辑:

下面是实例化ChatHistory并调用write的类:

namespace Vendor\Chat;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

use Vendor\Chat\Client;
use Vendor\Chat\Message;
use Vendor\Chat\ChatHistory;

use Illuminate\Database\Model;

class Chat implements MessageComponentInterface {

    protected $app;

    protected $clients;

    public function __construct() 
    {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) 
    {
        $client = new Client;
        $client->setId($conn->resourceId);
        $client->setSocket($conn);

        $this->clients->attach($client);
    }

    public function onMessage(ConnectionInterface $conn, $msg) 
    {
        $message = new Message($msg);

        $history = new ChatHistory;
        ChatHistory::create(array(
                     'room_token' => $message->getRoomToken(),
                     'user_id' => $message->getUserId(),
                     'message' => $message->getMessage(),
                  ));
        /* error here */
        /* ... */ 
    }

    public function onClose(ConnectionInterface $conn) 
    {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) 
    {
        $conn->close();
    }

    protected function getClientByConn(ConnectionInterface $conn)
    {
        foreach($this->clients as $client) {
            if($client->getSocket() === $conn) {
                return $client;
            } 
        } 

        return null;
    }
}

DB不可用的事实表明Eloquent没有被加载到顶层?

推荐答案

Answer:

使用服务Provider 的boot方法 bootstrap 包.


Explanation:

由于您正在开发一个与Laravel一起使用的包,所以没有必要创建自己的Capsule个实例.你可以直接用Eloquent.

您的问题似乎源于代码运行时DB/Eloquent尚未设置.

您还没有向我们展示您的服务Provider ,但我猜您正在使用一个,并使用register方法完成所有操作.

由于您的包依赖于不同的服务Provider (DatabaseServiceProvider)在其自身执行之前进行连接,因此 bootstrap 包的正确位置是在服务Provider 的boot方法中.

下面是the docs的一句话:

register方法在服务提供者注册时立即调用,而boot命令仅在请求路由之前调用.

因此,如果您的服务Provider 的操作依赖于另一个已经注册的服务Provider […]你应该使用boot法.

Laravel相关问答推荐

警告:构建Reaction App(VITE)后无法解码下载的字体警告

laravel错误更新使用外键的数据库

如何在生产中的 laravel 应用程序中获取标头请求值?

如何在 laravel 5.2 中显示 500 内部服务器错误页面?

如何在 Laravel 中显示渲染时间/页面加载时间?

Laravel 没有定义

如何更改 Laravel5 中的视图文件夹?

Laravel Blade 没有额外的空格?

Laravel 使用 Storage::put 生成唯一 ID

php Laravel-遇到格式不正确的数值(在字符串上)

如何在 Laravel 5 中验证当前、新密码和新密码确认?

Laravel Artisan CLI 安全地停止守护进程队列工作者

Laravel:命令未找到

Laravel 的 toSql() 方法是否会屏蔽 id? (列值被问号替换)

将 Laravel 集合附加到另一个集合

如何在 Laravel 中正确安装软件包?

如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件

在 Laravel 中包含 bootstrap 程序

在我的编辑器中管理上传文件的最佳方式?

Laravel 数据迁移