Home > Net >  erro laravel "Undefined property: Illuminate\Support\Facades\Request::$email"
erro laravel "Undefined property: Illuminate\Support\Facades\Request::$email"

Time:07-09

I'm doing a login and registration screen, the registration screen is perfect, but the login screen is giving me a headache to authenticate.

the registration is done, but as soon as I log in it gives this error...

"Undefined property: Illuminate\Support\Facades\Request::$email"

I don't know what else to do to make it work.

CONTROLLER:

<?php

namespace App\Http\Controllers;


use App\Models\Usuario;
use Illuminate\Support\Facades\Auth;
use Request;


class Usuarios extends Controller
{
    public function cadastrar()
    {
        $usuario = new Usuario(Request::all());
        $usuario->save();
        return redirect('/')->with('mensagem_sucesso', 'Cadastro efetuado com sucesso!');

    }

    public function index()
    {
        return view('layout/cadastrousuario');
    }

    public function indexlogin()
    {
        return view('layout/login');
    }

    public function logar(Request $request)
    {

        if (Auth::attempt(['email' => $request->email, 'password' => $request-> password])) {
            dd('voce esta logado');
        } else {
            dd('voce nao esta logado');
        }

    }

}

MODEL:

<?php

namespace App\Models;

use App\Models\Model\Request;
use DB;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;

class Usuario  extends Authenticatable
{

    protected $table = 'usuario';
    public $timestamps = false;

    protected $fillable =
        array(
            "codigo",
            "nome",
            "email",
            "apelido",
            "senha",
            "bloqueado",
            "saldo",
            "saldo_atual",
            "admin"
        );


    use HasFactory;
}

ROUTE:

<?php

use App\Http\Controllers\Lancamentos;
use App\Http\Controllers\LancamentosSimplificado;
use App\Http\Controllers\Usuarios;
use Illuminate\Support\Facades\Route;

// Route = (rota)::get ou post = (method) ( '/home' = (link) , [Lancamentos = (controller) :: class, 'logar' = ( function) ;

Route::get('/', [Lancamentos::class, 'index']);
Route::get('/salvar', [Lancamentos::class, 'salvar']);
Route::get('/maisdetalhes/{codigo}', [Lancamentos::class, 'maisdetalhes']);
Route::get('/criarchat', [Lancamentos::class, 'criarchat']);
Route::post('/cadastrar', [Lancamentos::class, 'cadastrar']);
Route::post('/cadastrar-simplificado', [LancamentosSimplificado::class, 'cadastrar']);
Route::get('/criarchat', [LancamentosSimplificado::class, 'listar']);
Route::get('/chat/{codigo}', [Lancamentos::class, 'chat']);
Route::get('/chatcriado/{codigo}', [LancamentosSimplificado::class, 'chatcriado']);
Route::get('/cadastrar-usuario', [Usuarios::class, 'index']);
Route::post('/cadastrar-usuario', [Usuarios::class, 'cadastrar']);
Route::get('/login', [Usuarios::class, 'indexlogin']);
Route::post('/login', [Usuarios::class, 'logar']);

page image as soon as I click login

CodePudding user response:

to start you have to make validations in the register function to be sure that the email address arrives well and is registered. i would start by modifying this function

public function cadastrar(Request $r)
 {
   $r->validate([
     'name' => 'required|string',
     'email' => 'required|email|unique:users',
     'password' => 'min:6',
     'password_confirmation' => 'required_with:password|same:password|min:6',
     'custom_field' => 'custom validation'

 ]);
 $input = $r->all();
 $input['password'] = Hash::make($r->password);
 $utilisateur = Model::create($input); //the Model == Usuario;
 return redirect('/')->with([
    'message'    => "Cadastro efetuado com sucesso!",
    'alert-type' => 'success',
]);
}

this is just a code snippet, I don't pretend to say that it's exactly what you need.the next way is the login function

if (Auth::attempt(['email' => $r->email, 'password' => $r->password])) {
                // The user is active, not suspended, and exists.
                $user = Auth::user();
                if($user->fcm_token != Null){
                    $token = $user->createToken('AUTH')->accessToken;
                    $user->remember_token = $token;
                    $user->device_token = $user->fcm_token;
                    $user->save();
                    $response = [
                        "data"=> [
                            'user'=> $user,
                            'token'=> $token,
                        ],
                        'message_fr' => 'Utilisateur connecté avec succès',
                        'message_en' => 'User logged successfully',
                    ];
                    return response()->json($response, 200);
                }else{
                    $response = [
                        'message_fr' => 'Vous êtes peut-être un robot',
                        'message_en' => 'You may be a robot',
                    ];
                    return response()->json($response, 422);
                }
            } else {
                $response = [
                    'message_fr' => 'Veuillez vérifier vos informations de connexion',
                    'message_en' => 'Please check your login information',
                ];
                return response()->json($response, 422);
            }

since you put a validation on the register, you are sure that the email is not only present, but also conforms to the nomenclature of an email

these two methods presented are examples taken from my source code of an available project, Good luck to you

CodePudding user response:

You are using the wrong Request class. Request (Illuminate\Support\Facades\Request) that is aliased in config/app.php is the Facade, static proxy, for the bound Request class instance, Illuminate\Http\Request. If you want an instance of a Request you need to be using Illuminate\Http\Request.

use Illuminate\Http\Request;

Now via dependency injection you will have an instance of the Request class (which has magic methods to access inputs via dynamic properties). If you keep what you have then you would not be asking for an instance via dependency injection and would have to use the Facade as a Facade:

public function logar()
{
    ...
    $something = Request::input(...); // static call to Facade
    ...
}
  • Related