Hello I wanted to ask regarding the Email Notification on Laravel, I have made a Status Notification wher I added this code
public function toMail($notifiable)
{
$leaveStatus=$this->leave->status;
if($leaveStatus==1){
$leaveNotify=" approved ";
}
else{
$leaveNotify=" declined ";
}
return (new MailMessage)
->subject('Leave Status')
->greeting('Hello '.$this->leave->user_id)
->line('I hope you are doing well')
->line('Your requested leave of' . $this->leave->type. ' type has been ' .$leaveNotify)
->line('Leave type: ' .$this->leave->type)
->line('From dt :' .$this->leave->from)
->line('To dt :' .$this->leave->to);
}
This works very well the email is being sent in each change on a Leave Application so what I am looking for is the part of the greeting , the
greeting('Hello '.$this->leave->user_id)
It shows the ID of the user instead of the first_name(which is a filed for the name) I have tried adding a ->first_name after the user_id but then it returns an error, the user_id doesn't have a foreign key that connects it with the users table its just a field which stores the id of each authenticated users but it works all the way here so im not sure that is the problem
You can use the notifiable variable passed into the function, which is the user the email is being sent to.
->greeting('Hello '.$notifiable->first_name)
You need to add relation for user in that model. See https://laravel.com/docs/7.x/eloquent-relationships . After that call it via $this->leave->user->first_name
Related
When the user registers, an email will be sent to confirmation
But how do I send a notification when a user updates their email?
Like when he registers
if you want to reverify your user's email , you should set his email_verified_at to null then call sendEmailVerificationNotification method .
in your user model (located in app\user) add this method :
public function sendNewVerification() {
$this->email_verified_at =null;
$this->sendEmailVerificationNotification();
}
and call it in your controller .
hope that would work.
I am new here.
I have a project in Laravel. I have one textarea and data from it is save in datavase. It works good. Now I would like to send automatical email to one specific email address with this data. It must be sent only one time with save to database.
I have no problem with sending email to customer with data but now I need to send email with data from this textarea to one specific email. It is a textarea what we have to buy for customer. It must be sent to our cooperation company.
Is it possible?
Ofcourse this is possible!
You should take a look at the following resources :
Observers
https://laravel.com/docs/6.0/eloquent#observers
Notifications
https://laravel.com/docs/6.0/notifications
-> specifically : https://laravel.com/docs/6.0/notifications#mail-notifications
yes, you can just trigger your function after saving: for example, after saving in controller.
public function store(Request $request){
$var = new Property; //your model
$var->title=$request->title; // the input that being save to database.
$var ->save();
// Send email to that input
Mail::send('email',['email'=>$request->title],function ($mail) use($request){
$mail->from('info#sth.com');
$mail->to($request->title);
});
return redirect()->back()->with('message','Email Successfully Sent!');
}
Ok, now I know that if I use file driver for session, the sessions are going to be stored in /storage/framework/sessions , when I echo the Session::getId() I get the session Id that is stored in the storage.
when I open that session, it look like something like this:
a:7:{s:6:"_token";s:40:"hgG3c6DQ5XHEvwK7925CmfGUK54dhFPIWB2kFCUz";s:3:"url";a:0:{}s:9:"_previous";a:1:{s:3:"url";s:27:"http://e-learning.local/p2p";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:50:"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d";i:6;s:3:"key";s:8:"smothing";s:22:"PHPDEBUGBAR_STACK_DATA";a:0:{}}
This is localhost development so no warry to publish the session
I wounder, there is no user id or user name at all !, how laravel get the auth user from this session !
what is the value needed to be matched in order to know the user ?
e.g Auth::id() get the user id from session, but how ! if there is no indicator in the session about the user id or something :\
furthermore, if I have the session id for the user, and I can't use Auth::id() to get the user id (due to fact that I'm building another app using another framework inside laravel, but they should have the same users) , can i get the user from the session ?
In my application i have found user_id listed as value for this dynamic key "login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d"
This is what i run to get sessions of all users & identify their session id, along with user id:
foreach (glob(base_path()."/storage/framework/sessions/*") as $filename) {
foreach (unserialize( file_get_contents($filename) ) as $key => $value) {
if(substr_count($key, 'login_web') > 0){
echo "Session ID: " . basename($filename) . " - User ID: ".$value."<br>";
}
}
}
I want help in scaffolding code with Laravel default Mail package to send an email to the recipient with an enhancement that checks the status either mail is delivered to recipient and then check that either recipient opened the mail or not and then change the status of that email in my db_email_list. I googled it that to add headers just like follow the example but could not get it how to get the status
$message->getHeaders()->addTextHeader('X-Confirm-Reading-To','recipient_mail');
$sendEmail->getHeaders()->addTextHeader('Disposition-Notification-To','recipient_mail');
$sendEmail->getHeaders()->addTextHeader('Return-Receipt-To','recipient_mail');
When the user has gotten the email: Simply use this piece of code:
if (count(Mail::failures())) {
return false;
} else {
return true;
}
true=delivered, false=not delivered
When user reads the email: Hm sounds like you need to include a trick in your email in order to know if user has opened/read the email by simply adding for instance including an image on your email with a route defined in your end and passing user id as query param.
<img src="http://www.example.com/user-read-email?user_id=20" />
So whenever the user opens the email img src will fire a call to your url and simmply get the user id from the url, and set the flag for that user in db.
I have a web app i'm working on.Users can create patients, which have a unique id. Problem I have is that when another user logs in, he can easily access patients not assigned to him by simply inputing their id in the url. Please how do i solve this? Heres a sample of my route for the
user to view his patient:
Route::get('patients/{patient}/view', 'Portal\PatientController#viewPatient');
and in the Patientcontroller:
public function viewPatient($patient){
$patient = Patient::where('id', $patient)->first();
return view ('portal.patient',compact('patient'));
}
Please what am I doing wrong?
You can use policies for that:
Policies are classes that organize authorization logic around a particular model or resource. For example, if your application is a blog, you may have a Post model and a corresponding PostPolicy to authorize user actions such as creating or updating posts.
Or gates:
Gates are Closures that determine if a user is authorized to perform a given action
I'd use policies, but you also can manually check if a user can view a page with something like:
if (auth()->id() !== $patient) {
return redirect('/')->with('message', 'You can not view this page');
}
You could also keep GET to access to this page without inputing the id. For example, if you want to obtain patients only from the current user logged in :
web.php :
Route::get('patients/view', 'Portal\PatientController#viewPatient');
Patientcontroller :
public function viewPatient(){
$id = auth()->id();
$patient = Patient::where('id', $id)->first();
return view ('portal.patient',compact('patient'));
}
Keep in mind that this will work only with an authenticated user.
If your database table structure is like this
Patients
--------
id //Unique ID of Patient
user_id //User that created
patient
Then you can do the check in controller like.
public function viewPatient($patient)
{
$patient_check = Patient::where('id', $patient)->where('user_id','=',Auth::user()->id)->first();
if($patient_check == null || count($patient_check) == 0)
{
return "You cannot view this patient";
}
else
{
return view ('portal.patient',compact('patient'));
}
}
This is simple and yet does the work.