Class 'App\Http\Controllers\Api\Validator' not found - laravel

I tried to make a validator for API in laravel
But this error appears
Class 'App\Http\Controllers\Api\Validator' not found
How can I solve this?

Import the facade at the top of your class
use Illuminate\Support\Facades\Validator;
You can also call the Facade alias like so
\Validator::make.....
Anywhere in your code if you have the alias set in config/app.php (it is by default)
'aliases' => [
// Other aliases omitted for brevity
'Validator' => Illuminate\Support\Facades\Validator::class, // <--- Here
'View' => Illuminate\Support\Facades\View::class,
],
Hope this helps

Just use your controller
use Validator;

Related

Can I change Model User that used in auth system in laravel?

I tried to change User Model that used by default when use command php artisan ui:auth
to another model
but all of way to do that is not working
What should to do that ?
I am using laravel version 7.x
You can have different (or as many different) models as you want for various reasons. You just need to change the relevant section in config/auth.php. I always use a Models directory, so one of the first things I do with a new app is to relocate the User model and then tell Auth where to look for it:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
You also have to update the model's use statements in your controllers if you have custom logic, but for a vanilla Laravel Auth set-up, you should be good just by changing the config.
Edit: If you do have a structure like mine (a models directory) the default App\User namespace declaration has to change on the model itself:
Change:
namespace App;
To:
namespace App\Models;
Or whatever it is that matches your structure.

Laravel 6.5.2 error when registering on overwritten users table

I have overwritten the default user table with an existing table.
When trying to register a new user i get the following error:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\customUser given, called in {path}\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 35
To overwrite the default users table with my own I have made the following changes:
///Auth.php///
//Auth defaults:
'defaults' => [
'guard' => 'web',
'passwords' => 'customusers',
],
//Auth guards:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'customusers',
],
//User providers:
'providers' => [
'customusers' => [
'driver' => 'eloquent',
'model' => App\customUser::class,
],
I don't know if these changes could cause the error I'm facing.
The error is supposedly fired in this part of 'RegisterUsers.php'
$this->guard()->login($user);
This is part of the function 'register', which looks like this:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
Any help regarding this error would be kindly appreciated, I'm fairly new to Laravel so I would like to recieve a clear explanation to my problem so I can understand why it is calling this error.
Kind regards,
geertjanknapen
Your model needs to extend Illuminate\Foundation\Auth\User, otherwise you cannot use it with the authentication methods like login / logout etc.
In your customUser model add the following:
use Illuminate\Foundation\Auth\User as Authenticatable;
class customUser extends Authenticatable
I would also recommend to rename your class customUser to CustomUser in order to follow PSR-1:
Class names MUST be declared in StudlyCaps.
The term ‘StudlyCaps’ in PSR-1 MUST be interpreted as PascalCase where
the first letter of each word is capitalized including the very first
letter.
On RegisterUsers.php you need to include
use Illuminate\Foundation\Auth\AuthenticatesUsers;
and below class RegisterUsers extends Controller
you need to add
use AuthenticatesUsers;

Textarea in laravel 5.4

I have to print the reponse of API in textarea UI (blade.php) in laravel 5.4.
Tried doing:
{{ Form::textarea('response', '3 < 4') }}
But it gives the following error:
(1/1) FatalErrorException
Class 'Form' not found
What can I do to achieve this. In short I want an response textarea like it is in restclient.
Thanks !
You need to install Laravel FormCollective.
Run the following command from the Terminal: composer require "laravelcollective/html":"^5.2.0"
Next, add your new provider to the providers array of config/app.php:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases array of config/app.php:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],
Then, you can use {{ Form::textarea('response', '3 < 4') }} in your blade file!
Hope you understand!
The Form class is not a part of the default install of Laravel 5.
Please refer to installation here: https://laravelcollective.com/docs/master/html

Laravel Loading Service Provider and Alias

Maybe it is just me but im not sure what this do and why they need to do this?
in the providers that are loaded in laravel they did this
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
...
same for alias
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
...
What does the ::class do and why can't they just leave it off like this?
'aliases' => [
'App' => Illuminate\Support\Facades\App,
'Artisan' => Illuminate\Support\Facades\Artisan,
It just return class name with namespace !
Since PHP 5.5, the class keyword is also used for class name resolution.
You can get a string containing the fully qualified name of the ClassName class by using ClassName::class.
This is particularly useful with namespaced classes.
namespace NS {
class ClassName {
}
echo ClassName::class;
}
The above example will output:
NS\ClassName
Source 1
Php manual

Getting ReflectionException while using a Package

I am using a Package for roles and permissions management in my laravel 5.1 application and getting error while trying to create roles by using following code.
<?php
use Bican\Roles\Models\Role;
use Illuminate\Database\Seeder;
class RoleTableSeeder extends Seeder
{
public function run()
{
$adminRole = Role::create([
'name' => 'Admin',
'slug' => 'admin',
'description' => '', // optional
'level' => 1, // optional, set to 1 by default
]);
}
}
and I am seeding it using following command on CLI.
php artisan db:seed --class=RoleTableSeeder
but unfortunately I am getting ReflectionException
"[ReflectionException] Class RoleTableSeeder does not exist"...
What might be wrong with it? Looking for your help...
Link to Package: https://github.com/romanbican/roles
Problem solved using the following command on CLI.
composer dump-autoload
Acknowledged: Package Owner

Resources