Laravel Reset Password Reminder , passing variables to Blade template - php

Hi I have the following code in my PasswordController.php but its not working?
// get the user info..
$user = DB::table('users')
->where('email', '=' , Input::get('email'))
->first();
if (empty($user))
return Redirect::back()->with('message', "email doesn't exsist!");
// set email template
Config::set('auth.reminder.email', 'emails.auth.admin-reminder');
$response = Password::remind($credentials, function($message) use ($user){
$message->with('name'=> $user->first_name);
$message->subject(trans('assword Reset'));
});
I am trying to pass $user->first_name to my blade email template? How do I do that?
Thanks

Figured it out ,
Before
Password::remind
we add this :
View::composer('emails.auth.admin-reminder', function($view) use ($user) {
$view->with(['name' => $user->first_name]);
});
You can pass to the view as many variables as you want

Related

Login a user only if the status = '1' in laravel 5.2

I am working with laravel 5.2 and i want to login the user if the status is active
Route::get('/ProductInquiry', function(){
$id = session('esysid');
$user = UsersModel::where('employeeID', 'LIKE', $id)->get();
session(['name' => $user[0]->sAMAccountName]);
return view('home');
})->name('home');
I have status column in my database and I need help to authenticate the active users by getting the user's status here in routes, so I can know who are the active users of the system. I expect to login the users which have a status = 1.
You can use Laravel Where Clause the achieve your result
Route::get('/ProductInquiry', function(){
$id = session('esysid');
$userDetail = UsersModel::where('employeeID', $id)->where('status', 1)->first();
session(['name' => $userDetail->sAMAccountName]);
return view('home');
})->name('home');
Also, As the employeeID is unique so you can use first in place of get method.
get() - Return a collection.
first() - Return a single object.
You should rethink about checking user informations
$user = UsersModel::where('status', 1)->find($id)->get();
or
$user = UsersModel::whereStatus(1)->find($id)->get();
Try:
Route::get('/ProductInquiry', function(){
$id = session('esysid');
$user = UsersModel::where('employeeID', $id)->whereStatus(1)->first();
session(['name' => $user->sAMAccountName]);
return view('home');
})->name('home');

how to pass dynamic content(content which is coming from database) to send mail in laravel 5.4

I m new to laravel.i have table email_template and want to send mail to user when user forgot password.i m fetching content dynamically from database but i dont know how to pass it to mail function in laravel.
Mail::send($posts['email_template'], ['USER' =>$post['user] ], function($message)
{
$message->from('test#gmail.com')->subject('Welcome to laravel');
$message->to('test8#gmail.com');
});
where $posts['email_template'] is a content which i want to send and user is a variable which i want to replace in content
Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
$message->from('test#gmail.com', 'Your Application');
$message->to('test8#gmail.com', $user->name)->subject('Welcome to laravel');
});
emails.template is your view - template.blade.php file - /resources/views/emails/template.blade.php
Now, in your view i.e emails.template, you can do:
{{ $user->name }}, {{ $data->address }}
You can Define
ADMIN_EMAIL and CC_EMAIL in the constant file in the config folder
$emailData = array(
'name'=>'toName',
'toEmail'=>$request->email
);
$this->sendEmail($emailData);
Email Function
function sendEmail($emailData){
$this->adminEmail = config('constant.ADMIN_EMAIL');
$this->ccEmail = config('constant.CC_EMAIL');
$this->toEmail = $emailData['toEmail'];
$this->emailTemplate = $emailData['emailTemplate'];
$data['emailInfo'] = array(
'name'=>$emailData['name']
);
Mail::send('emails.yourTemplate', $data, function ($message) {
//$message->attach($pathToFile);
$message->from($this->adminEmail, 'Laravel Email Test');
$message->to($this->toEmail)->cc($this->ccEmail);
});
}

Cannot show data from controller to email blade

I want to show $data form controller to view but it shows this error message. Do you know why and how to fix that? Thank you.
Here's my code:
UserController.php
public function forgotPassword(Request $request){
$data['user']['email'] = $request->input('user.email');
Mail::send('vendor.notifications.resetpassword', $data, function($message) use($data){
$message->from('bemsadmin#gmail.com');
$message->to($data ['user']['email']);
$message->subject('Your Email');
});
return response()->json($data);
}
resetpassword.blade.php
<!DOCTYPE html>
<html>
<body>
<p>{{$data['user']['email']}}</p>
</body>
</html>
the error happens beacuse you did not pass a variable called $data to the email view,
you pass a variable like this
Mail::send('vendor.notifications.resetpassword', ['data' => $data], function($message) use($data){
but with your code what get passed is
['user' => ['email' => 'email#examle.com']]
so try in your view to use the following code, it should do the job.
{{$user['email']}}

Extract user email address in Laravel with Artisan::call

I want to know how can I put $user->email in the following code snippet, where the email address is. Everything works well when hard coded in but not when I put $user->email where the email address is - I get error then. Please help. thanks.
public function runCommand(Request $request){
$user = User::all();
$signature = $request->input('signature');
$command = Artisan::call($signature, ['user' => 'myemail#email.co.za']);
return response($command);
}
$user = User::all();
returns a Collection
The following illustrates how you iterate over all user emails:
$users = User::all();
foreach ($users as $user) {
echo $user->email;
}
( In case you want to send an email to the currently authenticated user (?) from within a Controller, not a Command see here. )

Laravel Mail::send how to pass data to mail View

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!');
});

Categories