Laravel 5 - Class 'DB' not found - laravel-5

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.

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]
);

As previously advised First use
use Illuminate\Support\Facades\DB;
then go /bootstrap/app.php and uncomment
$app->withFacades();

try using this
use Illuminate\Support\Facades\DB;
instead of
use DB;

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

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

Related

Class "App\Http\Controllers\Auth\Mail" not found

How can I resolve this error? I am trying to customize the default email template on laravel. This is the code for the controller that sends the email.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use App\Models\User;
use Illumunate\Auth;
class EmailVerificationNotificationController extends Controller
{
public function store(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);
}
Mail::send('email.template', $request->user(), function($mail) use($data){
$mail->to($request->user()->email, 'no-reply')->subject("Verify Email Address");
$mail->from('admin#raketlist.com','testing');
});
$request->user()->sendEmailVerificationNotification();
return back()->with('status', 'verification-link-sent');
}
}
Add use Illuminate\Support\Facades\Mail; to other uses.

Laravel ViewComposer - Undefined variable: countUnreadNotifications

In my Laravel-5.8, I am trying to use ViewComposers so that I can display data in layouts\header
App\http\View\Composers\NotificationsComposer
<?php
namespace App\http\View\Composers;
use App\Models\Notification\UserNotification;
use Illuminate\View\View;
use Illuminate\Support\Facades\Auth;
class NotificationsComposer
{
public function compose(View $view)
{
$userCompany = Auth::user()->company_id;
$userID = Auth::user()->id;
$countUnreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->count();
$unreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->take(5)->get();
$allNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->get();
return $view->with([
'countUnreadNotifications' => $countUnreadNotifications,
'unreadNotifications ' => $unreadNotifications,
'allNotifications ' => $allNotifications
]);
}
AppServiceProvider
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Models\Notification\UserNotification;
use App\Http\View\Composers\NotificationsComposer;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer(['layouts.header'], NotificationsComposer::class);
}
}
layouts\header.blade
<span class="dropdown-item dropdown-header">You have {{ $countUnreadNotifications }} unread notifications</span>
When I logged in I got this error:
Undefined variable: countUnreadNotifications (View: C:\xampp\htdocs\resources\views\layouts\header.blade.php)
How do I resolve this?
The error is here 'unreadNotifications '. the is a space before the parenthesis. So I removed the space 'unreadNotifications' and everything works fine. Thank you

Laravel 5.3 How to use Braintree\WebhookNotification

I am using Laravel 5.3 and trying to add new webhook event handler in WebhookController
Here is my controller
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Log;
use App\Models\BraintreeMerchant;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(WebhookNotification $notification)
{
if( isset($_POST["bt_signature"]) && isset($_POST["bt_payload"]))
{
$notification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
$notification->kind == Braintree_WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED;
// true
$notification->merchantAccount->status;
// "active"
$notification->merchantAccount->id;
// "blue_ladders_store"
$notification->merchantAccount->masterMerchantAccount->id;
// "14ladders_marketplace"
$notification->merchantAccount->masterMerchantAccount->status;
}
}
}
but getting the following error message:
BindingResolutionException in Container.php line 763:
Target [Braintree\WebhookNotification] is not instantiable.
I've found the answer. this is how to implement Braintree webhook controller.
<?php
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Illuminate\Http\Request;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(Request $request)
{
$notification = WebhookNotification::parse($request->bt_signature, $request->bt_payload);
$merchantId = $notification->merchantAccount->id;
$result_merchant_status = $notification->merchantAccount->status;
}
}

Call to undefined method Illuminate\Notifications\Notification::send()

I am trying to make a notification system in my project.
These are the steps i have done:
1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost
In my AddPost.php file i wrote this code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class AddPost extends Notification
{
use Queueable;
protected $post;
public function __construct(Post $post)
{
$this->post=$post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
];
}
}
In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;
class PostNot extends Controller
{
public function index(){
$posts =DB::table('_notification')->get();
$users =DB::table('users')->get();
return view('pages.chat',compact('posts','users'));
}
public function create(){
return view('pages.chat');
}
public function store(Request $request){
$post=new Post();
//dd($request->all());
$post->title=$request->title;
$post->description=$request->description;
$post->view=0;
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
return redirect()->route('chat');
}
}
Everything was good until I changed this code:
$post->save();
to this :
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
It started to show an error which is:
FatalThrowableError in PostNot.php line 41: Call to undefined method
Illuminate\Notifications\Notification::send()
How can i fix this one please??
Thanks.
Instead of:
use Illuminate\Notifications\Notification;
you should use
use Notification;
Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.
Using this
use Illuminate\Support\Facades\Notification;
instead of this
use Illuminate\Notifications\Notification;
solved the problem for me.
Hope this helps someone.
using this is better
use Notification
Instead of
use Illuminate\Support\Facades\Notification
this makes the send() not accessible [#Notification Databse]

how to define multidimentional array globally in laravel controller?

I want to define multidimentional array in laravel controller globally.
I am defining it like this
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Communication_link;
use App\Contact;
use DateTime;
use App\Resource_status;
use App\Inquiry;
use App\Contact_communication;
use App\Pincode;
use App\City;
use App\User;
class createInquiryController extends Controller
{
public $response;
$map = array(
array("contact","id"),
array("communication_link", "id"),
array("contact_communication","id")
);
public function contact_select(Request $request){
return $map;
}
}
but this is throwing a error "undefined map".
Define it and assign data in constructor:
protected $map;
public function __construct()
{
$this->map = array(
array("contact","id"),
array("communication_link", "id"),
array("contact_communication","id")
);
}
Then you'll have access to this variable from any method in this controller:
public function index()
{
$data = $this->map;
}

Resources