I made a crud user but I have a problem with the password hashing.
@extends('superadmin.layouts.app')
@section('content')
<div >
<div >
<!-- left column -->
<div >
<!-- general form elements -->
<div >
<div >
<h3 >New User</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form method="POST" action="{{ route('user.store') }}" enctype="multipart/form-data">
@csrf
<div >
<div >
<label for="exampleInputEmail1">Name</label>
<input type="text" placeholder="Enter Name" name="name" required>
</div>
@error('name')
<div >
<div >{{ $message }}</div>
</div>
@enderror
<div >
<label for="exampleInputFile">Image</label>
<div >
<div >
<input type="file" id="Image" name="image" required>
<label for="exampleInputFile">Choose file</label>
</div>
</div>
</div>
@error('image')
<div >
<div >{{ $message }}</div>
</div>
@enderror
<div >
<label for="exampleInputEmail1">Secret word</label>
<input id="secretword" type="text" name="secretword" value="{{ old('secretword') }}" required autocomplete="secretword" autofocus>
</div>
@error('secretword')
<span role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<div >
<label for="exampleInputEmail1">Telegram</label>
<input id="telegram" type="text" name="telegram" value="{{ old('telegram') }}" required autocomplete="telegram" autofocus>
</div>
@error('telegram')
<span role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<div >
<label for="exampleInputEmail1">Email</label>
<input id="email" type="email" name="email" value="{{ old('email') }}" required autocomplete="email">
</div>
@error('email')
<div >
<div >{{ $message }}</div>
</div>
@enderror
<div >
<label for="password" >{{ __('Password') }}</label>
<input id="password" type="password" name="password" required autocomplete="new-password">
</div>
@error('password')
<span role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<div >
<label for="password-confirm" >{{ __('Confirm Password') }}</label>
<input id="password-confirm" type="password" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<!-- /.card-body -->
<div >
<button type="submit" >Submit</button>
</div>
</form>
</div>
</div>
<!--/.col (right) -->
</div>
<!-- /.row -->
</div>
@endsection
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::latest()->paginate(50);
return view('superadmin.user.index', compact('users'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$usersall = User::all();
return view('superadmin.user.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'secretword' => 'required',
'email' => 'required',
'password' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = public_path('images/');
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}
User::create($input);
return redirect()->route('user.index')
->with('success', 'User created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::find($id);
return view('superadmin.user.edit', compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required',
'telegram' => 'required',
'secretword' => 'required',
]);
$input = $request->all();
$user = User::find($id);
$user->name = $request->name;
$user->telegram = $request->telegram;
$user->secretword = $request->secretword;
$user->save();
return redirect()->route('user.index')->with('success','User updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$users = User::find($id);
$users->delete();
return redirect()->route('user.index')->with('success', 'Utilisateur supprime');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Qirolab\Laravel\Reactions\Traits\Reacts;
use Qirolab\Laravel\Reactions\Contracts\ReactsInterface;
class User extends Authenticatable implements ReactsInterface
{
use HasApiTokens, HasFactory, Notifiable, Reacts;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'secretword',
'image',
'telegram',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function roles()
{
return $this
->belongsToMany(Role::class)
->withTimestamps();
}
public function users()
{
return $this
->belongsToMany('App\User')
->withTimestamps();
}
public function authorizeRoles($roles)
{
if ($this->hasAnyRole($roles)) {
return true;
}
abort(401, 'This action is unauthorized.');
}
public function hasAnyRole($roles)
{
if (is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
}
} else {
if ($this->hasRole($roles)) {
return true;
}
}
return false;
}
public function hasRole($role)
{
if ($this->roles()->where('name', $role)->first()) {
return true;
}
return false;
}
};
I would like to make a hash of the password but I can't. I saw in the documentation that it is necessary to do Hash::make() but I don't understand where I have to do this. Could someone put me the line of code that needs to be done for hash and or tell me where I need to put it? Thanks for the help
CodePudding user response:
A call to $request->validate()
returns an array
of validated data. So what you can do is save that return value to a variable, then access the password
element on the array
in order to hash it before providing everything to your User::create()
method.
$validated = $request->validate([
'name' => 'required',
'secretword' => 'required',
'email' => 'required',
'password' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$validated['password'] = Hash::make($validated['password']);
User::create($validated);
CodePudding user response:
try with this method bcrypt function is in laravel that give you a hash password easly.
$fields = $request->validate([
'name' => 'required|string',
'email' => 'required|string|unique:users,email',
'password' => 'required|string|confirmed'
]);
$user = User::create([
'name'=>$fields['name'],
'email' => $fields['email'],
'password' =>bcrypt($fields['password'])
]);