Search code examples
phphtmllaravelformsvalidation

How to get and pass the data from a form in Laravel, and check its validation with data in SQL database?


I'm new to Laravel. I have a login form for administrators written in Laravel, a model Admin, AdminLoginController and HTML. I want to get the data from it, and check with admin data in SQL database. For example, a person signs in the form with his/her email and password. After clicking submit button, the data will be compared to the data in the MySQL database (PHPMyadmin). If signing in successfully, it will be redirected to the view 'home'.

            <form
                action="{{
                    url('home/admin/',[$info->id])
                }}"
                method="post"
                role="form"
                style="
                    width: 900px;
                    display: flex;
                    flex-direction: row;
                    align-items: flex-end;
                "
            >
                @method('post')
                {{ csrf_field() }}
                <div
                    class="input-label"
                    style="
                        flex: 1;
                        display: flex;
                        flex-direction: column;
                    "
                >
                    <label for="email">Email:</label>
                    <label for="password">Password:</label>
                </div>
                <div
                    class="input-box"
                    style="
                        flex: 6;
                        display: flex;
                        flex-direction: column;
                    "
                >
                    <input type="text" id="email" name="email" step="any" min="0" max="100" value="{{$info->email}}"/>
                    <input type="text" id="password" name="password" step="any" min="0" max="100" value="{{$info->password}}"/>
                </div>
                <div>
                    <input type="submit" value="Submit"/>
                </div>
            </form>

I initialized the data variable $info:

<?php
    $info = [];
?>

Here is my controller:

public static function loginCheckAdmin(Request $request): bool
    {
        $email = $request->email;
        $password = $request->password;
        if (Admin::query()->find($email) === null)
        {
            return false;
        }
        else
        {
            $fetchedPassword = Admin::query()->where('email',$email)->get('password');
            if ($fetchedPassword === $password)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    public static function redirectAdminToHome(Request $request)
    {
        if (self::loginCheckAdmin($request) === true)
        {
            $email = $request->email;
            $id = Admin::query()->where('email',$email)->get('id');
            redirect()->to('home/admin'.$id);
            self::updateAdminLoginTime($request);
        }
        else
        {
            self::denyUserAccess($request);
        }
    }
    public static function denyUserAccess(Request $request)
    {
        echo("Invalid login process!");
    }
    public static function updateAdminLoginTime(Request $request)
    {
        $email = $request->email;
        $lastLoginTime = Carbon::now()->toDateTimeString();
        Admin::query()->where('email',$email)->update(['last_login_time',$lastLoginTime]);
    }

So I want to ask how to initialize $info because the server always says "Attempt to read property "id" on array", and how to validate the data with the database. Thanks for any help!


Solution

  • just use the breeze pakage for laravel basic login

    open cmd in root of the project

    • php artisan breeze:install
    • php artisan migrate
    • npm install
    • npm run dev

    it handles basic user registration and authentication.