Issue with Laravel Notifications - laravel

I have a problem with Laravel notifications. I try to give a user notification about something, but Laravel cannot find notification class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class NotificationController extends Controller
{
public function getNot(Request $request)
{
$user = Auth::user();
$user->notify(new NewPost('a'));
}
}
I've also created a notification with the name NewPost.php, the problem is:
Class 'App\Http\Controllers\NewPost' not found
this one, so in the User model already included Notifications and notifiable.

Add use statement before class definition.
use Illuminate\Http\Request;
use Auth;
use App\Notifications\NewPost;
I assume that you create notification by artisan, if no, then keep in mind that namespace could be different.

Related

Return value must be of type Laravel 10

I am working on a Laravel 10 project and trying to create a controller that displays records. However, I have run into a problem while attempting to do so :
App\Http\Controllers\StudentController::index(): Return value must be of type Illuminate\Http\Response, Illuminate\View\View returned
I have attached below what I have tried so far:
Student Controller
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Student;
class StudentController extends Controller
{
public function index(): Response
{
$students = Student::all();
return view ('students.index')->with('students', $students);
}
If I remove the Response class, the code works, but I need to implement the Laravel 10 standard. I am unsure how to solve this issue. Can you please provide a solution?
Routes
Route::resource("/student", StudentController::class);
Laravel utilized all of the type-hinting features available in PHP at the time. However, many new features have been added to PHP in the subsequent years, including additional primitive type-hints, return types, and union types.
Release notes.
If you are using view function, you need to use the Illuminate\View\View class for type-hinting.
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Models\Student;
class StudentController extends Controller
{
public function index(): View
{
$students = Student::all();
return view('students.index')
->with('students', $students);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\CatalogueModel;
use Illuminate\Contracts\View\View;
class StudentController extends Controller
{
public function index(): View
{
$data['students'] = Student::all();
return view('students.index')->with($data);
}
}

Laravel, Route not defined. yet it is

I'm developing an eccomerce site. and im getting this error on checkout. please help
here are the defined routes:
//payfast payment
Route::get('payment', 'PaymentController#confirmpayment')->name('confirmPayment');
Route::get('/payfast/success','PaymentController#success')->name('payment.success');
Route::get('/payfast/cancel','PaymentController#cancel')->name('payment.cancel');
and here is the Controller (destination route):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use NunoMaduro\Collision\Provider;
use App\Models\Cart;
use App\Models\Product;
use DB;
use Billow\Contracts\PaymentProcessor;
Class PaymentController extends Controller
{
public function confirmpayment(PaymentProcessor $payfast)
{
$cart = Cart::where('user_id',auth()->user()->id)->where('order_id',null)->get()->toArray();
$data = [];
When you use Laravel Named Route name(), you have to use it.
Route::get('payment', 'PaymentController#confirmpayment')->name('confirmPayment');
Instead of using
payment
replace it with
confirmPayment
change your HTTP method from get to post
like this:
Route::post('payment', 'PaymentController#confirmpayment')->name('confirmPayment')
it seems you don't have route named payment you only have
confirmPayment
payment.success
payment.cancel

Laravel - Notifications Controller can't use method user()

I'm trying to add notification system to my Laravel project. I' watched this video to understand the system : https://www.youtube.com/watch?v=iDDUxqpNgSc
Notification table, model et controller are created. Also, i have created the view with Vue.JS and Pusher. It's work well !
However, in the notification controller, when i try to user Auth::user() method it's return null. I read somewhere it's because the middleware 'auth' is not already load when the controller is.
This is my NotificationsController file :
<?php
namespace App\Http\Controllers;
use App\Notification;
use Illuminate\Http\Request;
use App\Idea;
use App\User;
use Illuminate\Support\Facades\Auth;
class NotificationsController extends Controller
{
public function __construct()
{
}
function get(){
$notification = Auth::user()->unreadNotifications()->get();
return $notification;
}
function read(Request $request){
Auth::user()->unreadNotifications()->find($request->id)->markAsRead();
}
}
Do you have any idea how to solve this ?
Thank's for your time !
The answer was not about the Auth::user (It's accessible). I just baldy defined the notifiable_type in my model. It was App\Idea, it should be App\User

Trigger to Pusher.com does nothing

I am trying to use vinkla/pusher on Laravel 5.1
This is what I've added to app.php:
Vinkla\Pusher\PusherServiceProvider::class as a service provider
'LaravelPusher' => Vinkla\Pusher\Facades\Pusher::class, as a facade.
Route:
Route::get('/api/bid', [
'uses' => 'APIController#bid'
]);
And this is the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\CurrentAuction;
use App\User;
use App\Bid;
use Session;
use LaravelPusher;
class APIController extends Controller
{
public function getCurrentAuction()
{
// snip...
}
public function bid(User $user) {
// Whole heap of things done with $user...
// snip...
$data['bids'] = 1;
LaravelPusher::trigger('bid_channel', 'NewBid', $data);
}
}
Calling that method does everything except trigger the pusher event.
I don't understand what I've done wrong.
Any help would be greatly appreciated. Thanks!
It appears that when using Laravel Homestead/Vagrant, pusher, broadcasting or anything like that doesn't want to work for me.
I pushed everything up to a live server and it worked without any code changes.

Routing in Laravel is not working

I am using laravel 5.0
I am trying to route the following. But it is not working
Route::post('accesscontrols/permissions', 'AccescontrolsController#permission');
I don't know what error in this.
It does not access permissions function in AccesscontrolsController
I have a function in AccesscontrolsController
public function permission()
{
$roles = DB::table('roles')->get();
$permissions = DB::table('permissions')->get();
return view('accesscontrols.permission', compact('roles', 'permissions'));
}
What I have did wrong?
Your route declaration should be made in app/Http/routes.php.
Also, make sure that your controller is within the App\Http\Controllers namespace and that it extends App\Http\Controllers\Controller.
Ex:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function permission()
{
...
Also, if you whish to test it in the browser (by typing "accesscontrols/permissions" in the address bar), you route should answer to the GET verb. Try to declare it using Route::get( instead.
You are returning a view in your method and you are not working with any POST data, which is strange. Are you sure you want POST request and not GET?

Resources