Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel/Eloquent:致命错误:对非对象调用成员函数connection()_Php_Laravel_Package_Eloquent_Composer Php - Fatal编程技术网

Php Laravel/Eloquent:致命错误:对非对象调用成员函数connection()

Php Laravel/Eloquent:致命错误:对非对象调用成员函数connection(),php,laravel,package,eloquent,composer-php,Php,Laravel,Package,Eloquent,Composer Php,我正在Laravel4中构建一个包,但在尝试访问db时,遇到了一个非对象错误,而db似乎是一个正确实例化的对象。以下是设置: 正在讨论的配置和类: composer.json: ... "autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations

我正在Laravel4中构建一个包,但在尝试访问db时,遇到了一个非对象错误,而db似乎是一个正确实例化的对象。以下是设置:

正在讨论的配置和类:

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"
        }  
    }
...
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);
    }

}
$message = new Message($msg);

$history = new ChatHistory;
$history->create(array(
                 'room_token' => $message->getRoomToken(),
                 'user_id' => $message->getUserId(),
                 'message' => $message->getMessage(),
              ));
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
班级:

...
"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"
        }  
    }
...
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);
    }

}
$message = new Message($msg);

$history = new ChatHistory;
$history->create(array(
                 'room_token' => $message->getRoomToken(),
                 'user_id' => $message->getUserId(),
                 'message' => $message->getMessage(),
              ));
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
呼叫:

...
"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"
        }  
    }
...
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);
    }

}
$message = new Message($msg);

$history = new ChatHistory;
$history->create(array(
                 'room_token' => $message->getRoomToken(),
                 'user_id' => $message->getUserId(),
                 'message' => $message->getMessage(),
              ));
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
错误:

...
"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"
        }  
    }
...
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);
    }

}
$message = new Message($msg);

$history = new ChatHistory;
$history->create(array(
                 'room_token' => $message->getRoomToken(),
                 'user_id' => $message->getUserId(),
                 'message' => $message->getMessage(),
              ));
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没有被加载到顶部?

@matpop和@TonyStark的思路是正确的:Capsule\Manager没有被启动

use Illuminate\Database\Capsule\Manager as Capsule;

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

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
我能够在开机后进行雄辩的演讲。我认为另一个解决方案可能是(但未经测试):


尝试包括DB facade以及雄辩的

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
…然后查看您是否有权访问
DB::table('chat\u history')


(还请注意,在您的课堂上,您对
使用照亮\数据库\模型;
的调用应该是
照亮\数据库\雄辩\模型;

回答

在服务提供商的
boot
方法中引导包


说明

因为您正在开发一个与Laravel一起使用的包,所以没有必要创建自己的
Capsule
实例。你可以直接使用
雄辩的

您的问题似乎源于
DB
/
雄辩
在代码命中时尚未设置

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

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

以下是一段引自:

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

因此,如果您的服务提供商中的操作依赖于另一个已注册的服务提供商[…],则应使用
boot
方法


在使用流明的情况下,可能会出现相同的问题。在这种情况下,只需取消注释:

// $app->withFacades();

// $app->withEloquent();

bootstrap\app.php

中,我做的很简单,只是忘记了取消注释
$app->withFacades();
$app->withelount()在my bootstrap/app.php中


现在可以正常工作了

在哪一行出现此错误?它是用于历史记录还是用于
$message
的方法。我们或当然不知道这一点,我们也不知道什么是道歉。调用ChatHistory上的create()时引发错误。Message是一个表示套接字消息的简单类。是否尝试像中所述引导Laravel?是否使用Laravel 4.1或4.2?@J.LaRosee除了
create()
用于静态使用之外,问题可能与
$history
对象无关。相反,连接解析器(模型的静态属性)似乎没有设置。如果您依赖DatabaseServiceProvider,它可能启动不正确。您是在开发一个使用Eloquent的独立软件包,还是在构建一个专门用于Laravel的软件包?这是一个专门用于Laravel的软件包。@matpop您的建议让我走上了正确的轨道,所以我想给您奖金。我不知道该怎么做,因为你还没有提供答案。谢谢你的赞赏,它的价值远不止这些,所以不用担心。事实上,在这个阶段,我认为还没有人值得赏金,因为现在看来你已经解决了胶囊的问题,而且我相信这不是为拉拉威尔专门设计的包装的最佳方式。如果所有设置都正确,则在“正常”服务提供程序(包括DatabaseServiceProvider)已启动后,这些包的服务提供程序将由底层Laravel应用程序加载,因此您不需要Capsule,也不需要显式调用
$app->boot()
(已调用)。您遵循了吗?对不起,如果我暂时不在那里,我的时区在大洋的另一边:)赏金来了!我猜我们只是在文件中遗漏了一些声明。。。一定要仔细阅读!使用胶囊“修复”了问题,但似乎不是最佳解决方案()。寻找引导软件包是我一直在寻找的,看起来像,虽然胶囊解决方案帮助我走上了正确的道路,向我展示了代码不是问题,我加载和使用代码的顺序是不正确的。这为我修复了它,但这是为Lumen。就是这样:)非常感谢。