Tuesday, February 1, 2022

[FIXED] Connection for a user

Issue

I have for my table 'Student' 5 fields id, name, firstname, email, phone.

My question: I would like the user to connect by putting his name what should I change?

public function index(Request $request)
    {   
        $user = $request->user();
        $students= Student::query()
        ->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
            $query->where('email', $user->email);
        })
        ->when($request->has('search'), function (Builder $query) use ($request) {
            $query->where('name', 'like', '%'.$request->input('search').'%');
        })
        ->paginate(5);



    return view('admin.students.index', compact('students'))
        ->with('display_search', $user->hasRole('admin'));
    }

Here:

$query->where('email', $user->email);

I have to put this?

  $query->where('name', $user->name);

Thank you for your help.


Solution

If you want to change the authentication credential from email to another column in the user's table, override the username function in the app/Http/Controllers/Auth/LoginController.php

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'name';
}


Answered By - Salim Djerbouh

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.