I have this function that registers users pretty much the default from laravel auth, and i added this send email function. But now im wondering how can i make a function that will send again email if they click "resend link" for example if they didnt recieve the first time.
Register function with the send email:
protected function create(array $data)
{
$user = Account::create([
'login' => $data['login'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'verifyToken'=> Str::random(40),
'active' => (env('CONFIRM_EMAIL', true)) ? 0 : 1
]);
$thisUser = Account::findOrFail($user->id);
$this->sendEmail($thisUser);
return $user;
}
And this is the sendEmail function
public function sendEmail($thisUser){
Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
}
Both functions work well, but sometimes when i register new user i dont get the link i need to delete it from database and re-register it.
Set new verify token on user or even reuse the old one. Then send the email again. Loading the user by email so they don't have to be logged in.
Route::post('users/verify', 'UserController#resend')
protected function resend(Request $request)
{
$user = User::where('email', $request->input('email'))->first();
$user->verifyToken = Str::random(40);
$user->save();
$this->sendEmail($user);
return $user;
}
A very basic example form to call the controller. They need to provide the email, as you don't know which user to resend too.
<form action=" {!! route('resendEmail') !!}" method="POST">
<label for="email">Your email</label>
<input type="text" id="email" name="email" value="example#email.com">
<input type="submit" value="Submit">
</form>
Related
I added a CRUD interface for my user's table, and instead of a delete button, I used a block button. Which blocks a user (sets bloque field in the database from 0 to 1). I added a new function in my controller called block which is supposed to do the job yet I get a MethodNotAllowedHttpException error every time I click the blocking button.
UserController
public function block($id)
{
$user = User::find($id);
$user->bloque = 1;
$user->save();
return redirect('/users')->with('success', 'Utilisateur bloqué');
}
The blocking HTML fragment
<form action="{{ route('users.block', $user->id)}}" method="get">
#csrf
<!-- #method('DELETE')-->
<button class="btn btn-danger" type="submit">Bloquer</button>
</form>
Routes
Route::get('/block', [
'uses' => 'UserController#block',
'as' => 'users.block'
]);
I think the problem is related to id value, It should be instantiated from $request object. Like:
public function block(Request $request)
{
$user = User::find($request->id);
$user->bloque = 1;
$user->save();
return redirect('/users')->with('success', 'Utilisateur bloqué');
}
When I try to open Studentregister page it opens the default register page of auth().
I'm new in Laravel I m having issues with syntaxes...
I have made only one User class having link with role class.
(User class is generated through $php artisan auth and foreign key for Role class is added to it.)
Now I want to register different users like student, teacher through studentRegister.blade.php or teacherRegister.blade.php. And I have to fix role_id in student page as 1 and in teacher role id as 2. So what will be the syntax.
created different route for student register and teacher register (web.php) ..
Route::get('student/register','Auth\RegisterController#registerStudent');
Route::get('teacher/register','Auth\RegisterController#registerTeacher');
added role variable and send it to view (Auth/RegisterController.php)
public function registerStudent()
{ $role_id = 1;
return view('auth.register',compact('role_id'));
}
public function registerTeacher()
{ $role_id = 2;
return view('auth.register',compact('role_id'));
}
setted value of hidden input with "role" name (Auth/Register.Blade.php);
<input id="role" type="hidden" name="role" value="{{$role}}">
change fillable variable in user.php so you can fill role field.
protected $fillable = [ 'name', 'email', 'password',role ];
added role to your create function on RegisterController
**(Auth/RegisterController.php)**.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'role' => $data['role'],
'password' => Hash::make($data['password']),
]);
}
In StudentRegister.blade.php I have added this
<input id="role_id" type="hidden" name="role_id" value="1">
In TeacherRegister.blade.php I have added this
<input id="role_id" type="hidden" name="role_id" value="2">
I'm using Laravel 5.6 to develop a website.
Currently, I want to write a test codes for the website. I'm also new to building a website in general and this is learning curve for me to learn what I'm doing wrong.
I created a Profile based on a User model and the Profile should only be editable by the authenticated User only.
The form is actually working without errors on the browser side but once i run phpunit, it will fail.
Test Script:
/** #test */
public function an_authenticated_user_can_view_the_profile_page()
{
// Generate fake instance of authenticated user
$this->be($user = factory('App\User')->create());
// Will get the URL
$response = $this->get('/profile/'.$user->name);
// Check whether the string exists
$response->assertSee('Personal details for '.$user->name);
}
Controller:
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(User $user)
{
return view('user.profiles.show', compact('user'));
}
public function update(Request $request)
{
$this->validate(request(), [
'company' => 'required',
'street' => 'required',
'city' => 'required',
'zip_code' => 'required',
'state' => 'required',
'country' => 'required',
'phone' => 'required',
]);
$profile = \Auth::user()->profile;
$profile->update($request->all());
return back()->with('success', 'Profile updated!');
}
}
View:
<div class="heading">
<h3 class="text-uppercase">Personal details for {{ $user->name }}</h3>
</div>
<form method="POST" action="/profile">
{{method_field('PATCH')}}
{{csrf_field()}}
<input type="hidden" value="{{ $user->profile->id }}" name="id">
<div class="col-md-6">
<div class="form-group">
<label for="company">Company</label>
<input id="company" type="text" class="form-control" name="company" value="{{ $user->profile->company }}" required>
</div>
</div>
</form>
Image of the commented out Form test:
Commented Form
Image of the not commented Form test:
Not commented Form
I am rather confused why my test is failing once I insert the form with a value tag. If i commented out the form or just remove the value tag, the test will pass.
Been searching for the few days and still can't find the right answer to this. Am i using the right Assertion? What am I missing here? Any inputs will help me to further understand this. Thanks!
I found the answer. It was actually the factory that I've created.
In the User model, every registration leads to creating an empty Profile.
This is the new way of how I write the test script:
/** #test */
public function an_authenticated_user_can_view_the_profile_page()
{
//Generate a fake profile
$profile = factory('App\Profile')->create();
// Assign it to the user
$user = $profile->user;
// Authenticate the user
$this->be($user);
// Will get the URL
$response = $this->get('/profile/'.$user->name);
// Check whether the string exists
$response->assertSee('Personal details for '.$user['name']);
}
How can i pass data from my Controller to my customized mail View ?
Here's my controller's send mail method :
$data = array($user->pidm, $user->password);
Mail::send('emails.auth.registration', $data , function($message){
$message->to(Input::get('Email'), 'itsFromMe')
->subject('thisIsMySucject');
Here's my emails.auth.registration View
<p>You can login into our system by using login code and password :</p>
<p><b>Your Login Code :</b></p> <!-- I want to put $data value here !-->
<p><b>Your Password :</b></p> <!--I want to put $password value here !-->
<p><b>Click here to login :</b> www.mydomain.com/login</p>
Thanks in advance.
Send data like this.
$data = [
'data' => $user->pidm,
'password' => $user->password
];
You can access it directly as $data and $password in email blade
$data = [
'data' => $user->pidm,
'password' => $user->password
];
second argument of send method passes array $data to view page
Mail::send('emails.auth.registration',["data1"=>$data] , function($message)
Now, in your view page use can use $data as
User name : {{ $data1["data"] }}
password : {{ $data1["password"] }}
for those using the simpleMail this might help :
$message = (new MailMessage)
->subject(Lang::getFromJson('Verify Email Address'))
->line(Lang::getFromJson('Please click the button below to verify your email address.'))
->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
$message->viewData['data'] = $data;
return $message;
The callback argument can be used to further configure the mail. Checkout the following example:
Mail::send('emails.dept_manager_strategic-objectives', ['email' => $email], function ($m) use ($user) {
$m->from('info#primapluse.com', 'BusinessPluse');
$m->to($user, 'admin')->subject('Your Reminder!');
});
Hello I am using Laravel ( new to this framework ) and the Zizaco/Confide package. I have managed to edit a lot with my own needs en preferences but I can't seem to solve this one problem about adding a new input field to create new users as admin
see form.day_of_birth field
The error says that the field is not filled altho it is ( I have checked with printing the array of sessions after submit on my screen )
In the AdminUserController I have changed the PostEdit() method and the PostCreate() method adding the following
public function postCreate()
{
$this->user->username = Input::get( 'username' );
$this->user->email = Input::get( 'email' );
$this->user->birthday = Input::get( 'd__day_of_birth__m' );
$this->user->password = Input::get( 'password' );
// The password confirmation will be removed from model
// before saving. This field will be used in Ardent's
// auto validation.
$this->user->password_confirmation = Input::get( 'password_confirmation' );
$this->user->confirmed = Input::get( 'confirm' );
// Permissions are currently tied to roles. Can't do this yet.
//$user->permissions = $user->roles()->preparePermissionsForSave(Input::get( 'permissions' ));
// Save if valid. Password field will be hashed before save
$this->user->save();
if ( $this->user->id )
{
// Save roles. Handles updating.
$this->user->saveRoles(Input::get( 'roles' ));
// Redirect to the new user page
return Redirect::to('admin/users/' . $this->user->id . '/edit')->with('success', Lang::get('admin/users/messages.create.success'));
}
else
{
// Get validation errors (see Ardent package)
$error = $this->user->errors()->all();
return Redirect::to('admin/users/create')
->withInput(Input::except('password'))
->with( 'error', $error );
}
}
I have practically the same on the sign up page for the users to create a own account and that works like a charm. Somehow here it doesn't seem to work..
In the view I have added this
<div class="form-group {{{ $errors->has('d__day_of_birth__m') ? 'error' : '' }}}">
<label class="col-md-2 control-label" for="day_of_birth">{{{ Lang::get('form.day_of_birth') }}}</label>
<div class="col-md-10">
<input class="form-control js__birthdaypicker" placeholder="{{{ Lang::get('form.day_of_birth') }}}" type="text" name="day_of_birth" id="day_of_birth" value="{{{ Input::old('day_of_birth', isset($user) ? $user->email : null) }}}"/>
{{{ $errors->first('d__day_of_birth__m', '<span class="help-inline">:message</span>') }}}
</div>
</div>
In the html you named the input day_of_birth not d__day_of_birth__m.