8

I have ChatController located in app/http/controllers like so:

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use DB;

class ChatController extends Controller implements MessageComponentInterface {

    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) 
    {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $conn, $msg) 
    {
        foreach ($this->clients as $client) 
        {
            if ($client !== $conn )
                $client->send($msg); 

            DB::table('messages')->insert(
                ['message' => $msg]
            );
        }
    }

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

    public function onError(ConnectionInterface $conn, \Exception $e) 
    {
        echo 'the following error occured: ' . $e->getMessage();
        $conn->close();
    }

}

And I have chatserver.php file in the root like so:

<?php
require  'vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Http\Controllers\ChatController;


$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new ChatController()
        )
    ),
    8080
);

$server->run();

If I remove

DB::table('messages')->insert(
                    ['message' => $msg]
                );

from the ChatController and start chatserver.php it works, but if I don't remove it then the server starts but as soon as I send a message I get this error:

Fatal error: Uncaught Error: Class 'DB' not found in C:\wamp\www\laraveltesting\app\Http\Controllers\ChatController.php:31

Why won't it use DB? I am extending the laravel controller.

1
  • @KhanShahrukh same crap Commented Jul 30, 2016 at 12:48

6 Answers 6

10

This one is better

use Illuminate\Support\Facades\DB;

Or you can use a slash('/') before DB like below

/DB::table('messages')->insert(
                ['message' => $msg]
            );
Sign up to request clarification or add additional context in comments.

Comments

2

try using this

use Illuminate\Support\Facades\DB;

instead of

use DB;

1 Comment

I get this: the following error occured: A facade root has not been set. - either way, I don't get why use DB isn't enough since I am extending the controller.
2

As previously advised First use

use Illuminate\Support\Facades\DB;

then go /bootstrap/app.php and uncomment

$app->withFacades();

Comments

1

for Laravel 5 and up simply just use this

use DB;

instead of

use Illuminate\Support\Facades\DB;

which is use for Laravel 4 version

Comments

1

change use Illuminate\Support\Facades\DB; to Use DB;

Comments

0

Add this in your test class file (before the class keyword) so your test can find the location of the Laravel application and create a new app.

if (!isset($app)) {
    $app = new \Illuminate\Foundation\Application(
          // E.g. if the current dir is app/tests/Unit/
          // Your path may differ
        realpath(__DIR__.'/../../')
    );
}

And in the test methods where you want to use DB, add global $app

public function testSomething()
{
    global $app;
    //
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.