Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Login sql error

Tags:

php

mysql

laravel

When I try to login on Laravel 5.4 I have the following error after entering my email and password on the login page

SQLSTATE[42S22]: Column not found: 1054 Champ 'id' inconnu dans where clause (SQL: select * from `acteur` where `id` = 21 limit 1)

It's not the right id name it should be id_biodic_acteurand not id

acteur is my user table

Here is my login page :

<head>
    <meta charset="utf-8">
    <title>Se connecter</title>
    <link href="/css/app.css" rel="stylesheet">
</head>
<body class="login">
    <img class="logo-img" src="/img/logo.jpg" alt="">

    <h1 class="title">LPO Extranet</h1>

    <form class="form-login" role="form" method="POST" action="{{ route('login') }}">
        {{ csrf_field() }}

        <div class="form-input-login{{ $errors->has('email') ? ' has-error' : '' }}">
            <label for="email" class="">E-Mail</label>
            <input id="email" type="email" class="form-email" name="email" value="{{ old('email') }}" required autofocus>

            @if ($errors->has('email'))
                <span class="">
                    <strong>{{ $errors->first('email') }}</strong>
                </span>
            @endif
        </div>

        <div class="form-input-pwd{{ $errors->has('password') ? ' has-error' : '' }}">
            <label for="password" class="">Mot de passe</label>
            <input id="password" type="password" class="form-pwd" name="password" required>

            @if ($errors->has('password'))
                <span class="">
                    <strong>{{ $errors->first('password') }}</strong>
                </span>
            @endif
        </div>

        <label>
            <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
        </label>

        <button type="submit" class="form-input-submit">
            Valider
        </button>

        <a class="" href="{{ route('password.request') }}">
            Mot de passe oublié ?
        </a>

    </form>
</body>

And i changed a little my login controller :

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class LoginController extends Controller
{

    protected $primaryKey = "id_biodiv_acteur";
    protected $table = "acteur";

    use AuthenticatesUsers;

    protected $redirectTo = '/pages/birds';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

}

My auth routes :

 public function auth()
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->post('logout', 'Auth\LoginController@logout')->name('logout');

        // Registration Routes...
        $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        $this->post('register', 'Auth\RegisterController@register');

        // Password Reset Routes...
        $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
        $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
        $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
        $this->post('password/reset', 'Auth\ResetPasswordController@reset');
    }

And here AuthentificatesUsers :

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

trait AuthenticatesUsers
{
    use RedirectsUsers, ThrottlesLogins;

    public function showLoginForm()
    {
        return view('auth.login');
    }

    public function login(Request $request)
    {
        $this->validateLogin($request);

        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }

    protected function attemptLogin(Request $request)
    {
        return $this->guard()->attempt(
            $this->credentials($request), $request->has('remember')
        );
    }


    protected function credentials(Request $request)
    {
        return $request->only($this->username(), 'password');
    }

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());
    }

    protected function authenticated(Request $request, $user)
    {
        //
    }

    protected function sendFailedLoginResponse(Request $request)
    {
        $errors = [$this->username() => trans('auth.failed')];

        if ($request->expectsJson()) {
            return response()->json($errors, 422);
        }

        return redirect()->back()
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors($errors);
    }

    public function username()
    {
        return 'email';
    }

    public function logout(Request $request)
    {
        $this->guard()->logout();

        $request->session()->flush();

        $request->session()->regenerate();

        return redirect('/');
    }

    protected function guard()
    {
        return Auth::guard();
    }
}

Thanks for your help !

like image 771
TuxxyDOS Avatar asked Apr 09 '26 16:04

TuxxyDOS


1 Answers

If you use the built in authentication functionality there is no need to specify a custom login() mehtod, simply do:

php artisan make:auth

And follow the documentation.

The Illuminate\Foundation\Auth\AuthenticatesUsers trait contains all the methods you need. To handle a login POST request you need to use the inbuilt postLogin() mehtod.

Simply change your post route:

 $this->post('login', 'Auth\LoginController@postLogin');

Have a look at all available methods in the docs.

like image 116
Javier Arias Avatar answered Apr 12 '26 06:04

Javier Arias