How to fix Class 'App\Http\Controllers\Notification' not found in laravel? - php

I have a Controller which listens to a new Schedule creation and sends the result back to the view via ajax. Inside of it I want to add a Notification to send email to the user once the Schedule cannot be completed due to a lack of resources at that specific date and time.
The problem is that I get the error below:
Class 'App\Http\Controllers\Notification' not found in /laravel/app/Http/Controllers/DadosAgendamentoController.php on line 89
The folder structure is this:
-app
-Http
-Controllers
DadosAgendamentoController.php
-Notifications
AgendamentoPendente.php
DadosAgendamentoController.php head code:
namespace App\Http\Controllers;
use Input;
use Request;
use App\Servicos;
use App\Disponibilidades;
use App\Estabelecimentos;
use App\HorariosEstabelecimento;
use App\Agendamento;
use App\User;
use App\Notifications\AgendamentoPendente;
line 88 and 89:
$user = User::where('id',1)->get();
Notification::send($user, new AgendamentoPendente(1));
Trough my Controller I can access all the classes above, but not the AgendamentoPendente
My goal is to send an email do the admin so he can suggest a new date and time to the client when the resources are not available at the desired date and time.
How can it be fixed? Can I access the class in this Controller? How?

Notifications may be sent in two ways: using the notify method of the Notifiable trait or using the Notification facade.
https://laravel.com/docs/5.3/notifications#sending-notifications
Option 1
You can use notify() method:
$user->notify(new AgendamentoPendente(1));
Also, make sure User class uses Notifiable trait:
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
Option 2
Using facade with full namespace:
\Notification::send($user, new AgendamentoPendente(1));

Add use Notification; in your controller
OR
alternatively, use \Notification::send($user, new AgendamentoPendente(1));

add this at the top of the controller:
use App\Notifications\AgendamentoPendente;
i was having the same problem and this fixed it

Also note that if you are using the facade, make sure your User queries the email field from your database
$users = User::select("email")->get();
\Notification::send($users, new AgendamentoPendente(1));

You have to use facades at the top
use Illuminate\Support\Facades\Notification;
You can refer to this tutorial
https://thecodingsolution.com/view/laravel-notofication-laravel-database-notification

You can pull in the notification library used Lumen 8.0:
"illuminate/notifications": "5.3.*"
into your composer.json then running composer update to pull in the notification libraries.
You will also need to add
$app->register(Illuminate\Notifications NotificationServiceProvider::class);
to your bootstrap/app.php
This process working for me.
Thanks

Related

Laravel8: Class not found despite defining it in the models

I used the model "clients.php" but when I load in the ClientsController
$clients= Client::all()->sortby('name');
I get the: Class "App\Http\Controllers\Admin\Client" not found
What would be the solution? I knew that if I create a "clients" model, then it should load as "client" in my function
You should use full namespace, for example (if Client model is stored inside Models directory):
$clients = \App\Models\Client::all()->sortby('name');
or you can add use App\Models\Client; at the beginning of the file after the namespace declaration and then just use
$clients = Client::all()->sortby('name');
You have to import the Client model. If you use Laravel 8 then :
use App\Models\Client;
$clients = Client::all()->sortby('name');
OR
If you use Laravel 7 or below then:
use App\Client;
$clients = Client::all()->sortby('name');
you can do .
use your client controller
use App\Models\Client;
then
$clients= Client::all()->sortBy('name');
OR
call direct model in your controller.
$clients = \App\Models\Client::all()->sortBy('name');
If you provided the namespace in the Controller and it's still not working,
open the terminal and type "composer dump-autoload". I do it every time i have a new Model.

How to use Laravels spatie/laravel analytics package

I just downloaded this package for Laravel.
spatie/laravel-analytics
Its a Google Anayltics package, and I followed all the steps for setting up an account. What I'm having trouble is calling the methods. For example when it says:
Here is an example to retrieve visitors and pageview data for the current day and the last seven days.
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
I tried doing this in my function like this:
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Spatie\Analytics\Analytics;
use Illuminate\Support\Collection;
class DashboardController extends Controller {
public function index() {
$analytics = Analytics::fetchVisitorsAndPageViews(Period::days(7));
dd($analytics);
return view('admin.dashboard-v2');
}
}
Its giving me errors like:
Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically
Am I missing something here? I couldn't find any specific documentation online except for the Github ReadMe file
If you want to use the facade to access the class, you'll need to change use Spatie\Analytics\Analytics; to use Analytics;. That should take care of that error.
If you are going to use Period::days(7) then you will need to add use Spatie\Analytics\Period; because that's an actual static method, not a facade.

Importing class in laravel 5.2

use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
do i need to write the above import class every time when i create a new controller?In laravel 4.2 it does automatically
is any other way to do this?
Yes, you do. Your app is under the namespace App;, so if you want to use the Input class, but you don't say use Illuminate\Support\Facades\Input, PHP will try looking for it under your namespace App; and an exception will be thrown since the class you're looking for probably won't be there.
Those are not a mandatory class.
When ever you are using those facedes you have to add
There is one way i know.
class something like CustomController
Add the all the common classed in that controller.
Now for every controller you can extend the new CustomController

Extending Laravel 5 PasswordBroker so that I can use another email function

I want to use my own method for the emailResetLink, because I want to send emails with styling and I'm using CssToInlineStyles.
First I tried extending the PasswordBroker:
in PasswordController changed use Illuminate\Contracts\Auth\PasswordBroker to App\PasswordBroker
created new empty class, that extends the PasswordBroker
namespace App;
use Illuminate\Auth\Passwords\PasswordBroker as PasswordBrokerBase;
class PasswordBroker extends PasswordBrokerBase {
}
And I got this error (the newly created class is empty):
Target [Illuminate\Contracts\Auth\UserProvider] is not instantiable.
Then I tried to create contextual binding with:
$this->app->when('Illuminate\Auth\Passwords\PasswordBroker')
->needs('Illuminate\Contracts\Mail\Mailer')
->give('App\Services\Mailer');
But nothing happened (it still uses the old Mailer).
Any ideas of how can I achieve the goal?

Laravel custom authentication

I'm starting to discover Laravel 5, so I might need a bit of your help to understand a few things.
First, I want to develop a login page. It seems that Laravel has a whole authentication system, and I guess I should use it.
Nevertheless, I want to display a login page (I know how to do this!), but after this, I would like to send the credentials to a server via an API call. The server will then tell me if the user is allowed to log in or not.
As far I understand Laravel and authentication, it seems that the authentication system works only with a local DB.
Can you confirm I need to use a custom authentication driver to do this?
I've been following this solution
but I get this error when loading my page:
FatalErrorException in CustomUserProvider.php line 6:
Interface 'Illuminate\Auth\UserProviderInterface' not found
Any help would be appreciated, feel free to ask me for more information if you need it.
Thanks
I tried following the same thread you mentioned and arrived at the very same results. Then I checked the implementation of native UserProviders (Illuminate/Auth/EloquentUserProvider and Illuminate/Auth/DatabaseUserProvider) and ended up using the same set as in EloquentUserProvider:
<?php namespace App\Auth;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class MyUserProvider implements UserProvider {
// Implementation goes here
}
I believe this to be more correct approach as the suggestions from the forum thread seem to be possibly for an older/beta version of L5.
Here is my CustomUserProvider file:
<?php namespace App\Auth;
use Illuminate\Contracts\Auth\UserProvider as UserProviderInterface;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\GenericUser;
class CustomUserProvider implements UserProviderInterface {
It's now working :-)

Categories