mercredi 4 mars 2020

Laravel user management system in Admin Panel

I,m building a user management system which I can add and edit and delete users from admin panel

yesterday everything works fine but today when I'm trying to login with any user in database I got this error in the email field

These credentials do not match our records.

Controller

class UsersController extends Controller
{
    public function index()
    {
        $users = User::all();
        return view('content.admin.user.index',compact('users'));
    }

    protected function create()
    {
        return view('content.admin.user.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, array(
            'name'         => 'required|string|max:255',
            'email'          => 'required|string|email|max:255|unique:users',
            'password'          => 'required|string|min:8',
        ));
        $password = Hash::make($request->password);
        $user = new User;
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = $password;
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(300, 300)->save( public_path('/images/avatars/' . $filename ) );

        } else {
            $user->avatar = 'user.jpg';
        }
        $user->save();
        if ($request['user']) {
            $user->roles()->attach(Role::where('name','User')->first());
        }
        if ($request['editor']) {
            $user->roles()->attach(Role::where('name','Editor')->first());
        }
        if ($request['admin']) {
            $user->roles()->attach(Role::where('name','Admin')->first());
        }

        return redirect('admin/users')->with('success', 'user is successfully saved');
    }

    public function edit($id)
    {
        $user = User::find($id);

        return view('content.admin.user.edit', compact('user'));
    }

    public function update($id, Request $request){
        $this->validate($request, array(
            'name'         => 'string|max:255',
            'email'          => "string|email|max:255|unique:users,email,$id",
            'password'          => "nullable |string|min:8,$id",
            'avatar'           => 'image|mimes:jpg,jpeg,gif,png,svg|max:2048',
        ));
        $password = Hash::make($request->password);
        $user = User::where('email',$request['email'])->first();
        $user->name = $request->input('name');
        $user->password = $password;
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(300, 300)->save( public_path('/images/avatars/' . $filename ) );
            $user->avatar = $filename;
        }
        $user->roles()->detach();
        if ($request['user']) {
            $user->roles()->attach(Role::where('name','User')->first());
        }
        if ($request['editor']) {
            $user->roles()->attach(Role::where('name','Editor')->first());
        }
        if ($request['admin']) {
            $user->roles()->attach(Role::where('name','Admin')->first());
        }
        $user->save();


        return redirect('admin/users')->with('success', 'user is successfully saved');
    }

    public function addRole(Request $request)
    {
        $user = User::where('email',$request['email'])->first();

        $user->roles()->detach();
        if ($request['user']) {
            $user->roles()->attach(Role::where('name','User')->first());
        }
        if ($request['editor']) {
            $user->roles()->attach(Role::where('name','Editor')->first());
        }
        if ($request['admin']) {
            $user->roles()->attach(Role::where('name','Admin')->first());
        }

        $user->save();

        return redirect('/admin');


    }

    public function destroy($id)
    {
        $user = User::find($id);
        $user->delete();
        return back();
    }
}

So any idea to fix that ??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire