Support the ongoing development of Laravel.io →
Authentication Database
Last updated 2 years ago.
0

You can customize the table on:

    database/migrations/***_create_users_table.php

To redirect the User model to a different table name you can set:

protected $table = 'bb_user'; 

on the user model

 app/User.php

Also in the user model change the fillable fields as you need them:

protected $fillable = [
    'name',  'lastname', 'usermail', 'userpassword',
];

To change the database name you can modify on .env file.

Keep in mind that if you do such changes you also need to modify the Views in:

resources/views/auth/login.blade.php
resources/views/auth/register.blade.php

changing the inputs like:

<div class="input-group">
    <input id="usermail" type="email" class="form-control {{ $errors->has('usermail') ? ' is-invalid' : '' }}" placeholder="{{ __('User Mail') }}" name="usermail" autofocus value="{{ old('usermail') }}">
    @if ($errors->has('usermail'))
        <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('usermail') }}</strong>
        </span>
    @endif
</div>

Same for 'userpassword'.

When you want to display the data you would do so:

{{ Auth::user()->usermail }}

In your blade templates.

Hope this helps. Happy coding.

Daniel

alfirus liked this reply

1

Such detail explanation as I wanted!

Will try it out.

@if $Daniel == "near" echo("French kiss") ; @endif

0

Lol happy to help, and BTW it would be

                @if ($Daniel == "near")
                    {{ "French kiss" }}
                @endif

But that's gay lol.

0

Hi,

I tried your solution and double check it. However i can't register. It back to register page without error. -.-"

0

Forgot to tell you about controllers, remember you are using a MVC architecture. In:

    app\Http\Controllers\Auth\LoginController.php
    app\Http\Controllers\Auth\RegisterController.php

add:

    public function usermail(){
        return 'usermail';
    }

The authenticate() method in LoginController would be:

public function authenticate(Request $request)
{
    $credentials = $request->only('usermail', 'userpassword');

    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->intended('/home');
    }
    if(!Auth::user())
    {
        return redirect('/login'); // add your desire URL in redirect function
    }
}

In the RegisterController the 'validator()' method would be:

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'useremail' => ['required', 'string', 'email', 'max:255', 'unique:users'], //<- HERE THE UNIQUE: IS THE USERS TABLE NAME
            'userpassword' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

And the create() method would be:

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'usermail' => $data['usermail'],
        'userpassword' => Hash::make($data['userpassword']),
    ]);

    return $user;
}

And I guess that's all.

Happy coding!

Daniel

Last updated 4 years ago.
0

Now we have a progress

https://flareapp.io/share/87nB6e7w#F58

It seems it still want to connect with "users" table.

0

Did you change name of the table on the migration?

    database/migrations/****_create_users_table.php

Check for your factory as well in

    database/factories/ModelFactory.php

Did you change the name of the table on.. ?

    app/User.php
    protected $table = 'users';

Also check this file

    config/auth.php

and change the field 'users'. If that doesnt work add this at the begining

/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'table' => 'users',

Make sure you run

    php artisan migrate:fresh --seed
Last updated 4 years ago.
0

Morning bro,

Yup, i checked all and get error

Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'alfirus_fldata.users' doesn't exist

It still keep trying to connect with users table

0

add this at the begining of config/auth.php

/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'table' => 'users',

this should do the trick.

0

Sadly that won't do the trick

Illuminate\Database\QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'alfirus_fldata.users' doesn't exist (SQL: select count(*) as aggregate from users where useremail = [email protected])

0

I need to see your code, not only the error. post it up and we'll deep into your problem.

0

Your email please

0

Use this please

        https://paste.larave​l.io

Just dont include the .env file

Last updated 4 years ago.
0

I'm using codeanywhere which i can share all files and structures, much better i guess

0

I search everywhere "users" but nothing found related. Weird isn't?

0

You got it now am i wrong?

I just register to your app!

Congrats. Can you tell me what whas the problem?

0

I delete everything and use default database and tables because I need to learn something.

Will create another sub domain to tackle the custom user database and tables later.

0

Cool bro,

Check this out.

             https://www.5balloons.info/changing-authentication-table-laravel/

Nice tutorial right there.

Happy coding. Daniel

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Alfirus Ahmad alfirus Joined 13 Dec 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.