I have some problems with my Laravel app.
I'm trying to send an email, but any time it sends it's not sending details that I need to pass to view.
I'm trying like (view)
Hello <strong>{{ $order['title'] }}</strong>,
<p>{{ $order['body'] }}</p>
But title and body are empty.
This is how controller looks like:
$order = [
'title' => 'title',
'body' => 'test body'
];
\Mail::to($user->email)->send(new OrderCreated($order));
And this is in mail
public $order;
public function build()
{
return $this->subject('Order Created')->view('emails.order');
}
What is wrong here?
Your problem is that you are not setting the $order property on your mailable.
You passing the order in when you do \Mail::to($user->email)->send(new OrderCreated($order)); so you just need to accept and set it in your mailable:
public function __construct($order)
{
$this->order = $order;
}
From there, the order will actually be accessible in your view as it is a public property.
Related
i looked everywhere for this solution but doesn't seem like there's a specific solution for php 8 as i understood this error is only a php error, so i am trying to send an email using a contact form and this is my code :
the controller :
public function _msg(Request $request , $id)
{
$info = entreprise::find($id);
//dd($info->mail);
$this->validate($request, [
'nom'=> 'required',
'email'=> 'required|email',
'telephone'=> array(
'regex:/^(?:(?:(?:\+|00)212[\s]?(?:[\s]?\(0\)[\s]?)?)|0){1}(?:5[\s.-]?[2-3]|6[\s.-]?[13-9]){1}[0-9]{1}(?:[\s.-]?\d{2}){3}$/',
'digits:10'
),
'message'=> 'required|min:10|max:500'
]);
$information = [
'name' => $request->nom,
'phone' => $request->Tel,
'email' => $request->email,
'message' => $request->message
];
//dd($information);
Mail::to('jon.reed10C#gmail.com')->send(new SendEmail($information));
$msg = new messages;
$msg->nom = $request->nom;
$msg->email = $request->email;
$msg->Tel = $request->Tel;
$msg->message = $request->message;
$msg->id_en = $request->id;
$msg->save();
return back()->with('success','Merci de nous contacter !');
}
mailable class :
class SendEmail extends Mailable
{
use Queueable, SerializesModels;
public $information;
public function __construct($information)
{
$this->info = $information;
}
public function build()
{
return $this->subject('Contact message')->view('email.msgSend');
}
}
the message view :
<h1>Message</h1>
>>>>>>>>>> <p>Nom: {{ $information['name']}}</p>
<p>Email: {{ $information['email']}}</p>
<p>Telephone: {{ $information['phone']}}</p>
<p>Message: {{ $information['message']}}</p>
the error points at the same place i am pointing at with the arrows in the message view, i tried dumping $information and it returns an array with the correct information and i dumped $information['name'] and it returned the name correctly, and i tried all the solution i found and nothing worked for me !
You have to change $this->info to $this->information
because you are assigning a value to a property that isn't declared yet. Your blade uses the values that are defined as properties. The property you defined remained empty, because you tried to add your value to a non existing property :)
I have set up notifications so that when a user comments on another users post the owner of the post receives a notification to let them know of the comment.
but at the moment the notification just says displays what is added to the return statement in the following:
public function toArray($notifiable) {
return [
'data' => 'Test notification'
];
}
But when a user posts a comment i want to replace the 'Test notification' with the following:
the name of the user that commented on the post saying for example:
'John commented on your post'
how do i do this so that it always displays the users name of the person thats commented.
if this helps i have passed in the user, comment and post at the top in the construct function like this:
public function __construct(User $user, Post $post, Comment $comment) {
$this->user = $user;
$this->comment = $comment;
$this->post = $post;
}
Usually the $notifiable will be the User. So you can use it if this is the case with your code.
If not, working with the code you provided you will do something like that
public function toArray($notifiable) {
return [
'data' => $this->user->username.' commented on your post',
'email' => $this->user->email
];
}
You can learn more about the $notifiable on the doc here https://laravel.com/docs/8.x/notifications#using-the-notifiable-trait
have a look at this article. it has answers to all of your questions.
the medium blog answer link
I'm using the Mail library in Laravel to send html email with custom data passed to a blade view.
The problem born when the mail has to render the html fetched from a row in the database which include a variable that i pass through the view.
This is my build function in my mailable class
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => $this->parameters->text,
]);
}
Then in the blade view:
<div>
{!! $text !!}
</div>
This is what the $text variable looks like:
<p>
<span>This is my text for the mail</span>
Click here to compile
</p>
The link href shoul contain the url variable value instead of not passing the variable name itself
A simple solution would be formating with php:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
]);
}
I did not try by myself but you could make an attempt with Blade::compileString(), i.e.:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => \Blade::compileString($this->parameters->text),
]);
}
What is the correct way to access $notifiable inside my view template?
I understand $notifiable is the user but when I have the following
public $abc;
public function __construct($abc)
{
$this->abc = $abc;
}
public function toMail($notifiable)
{
$mailMessage = (new MailMessage)
->from('xyz#xyz.com', 'xyz company')
->subject('xyz')
->markdown('emails.news-alert');
return $mailMessage;
}
Inside my blade template:
Hello {{ $notifiable->first_name }}
{{ $abc }}
The above throws an error because it doesn't recognize $notifiable
But if I pass it in as follows then it works:
$mailMessage = (new MailMessage)
->from('xyz#xyz.com', 'xyz company')
->subject('xyz')
->markdown('emails.news-alert', ['notifiable' => $notifiable);
Is $notifiable not a public property - I thought it was available to the view as default without needing to pass it through?
Yes you need to send the variables to use in the view
$mail->markdown(
'emails.news-alert', [
'notificable' => $notificable,
'abc' => $this->abc
]
);
some time you can use compact() helper but only when you have named variables (not $this->)
$abc = $this->abc;
$mail->markdown('emails.news-alert', compact(['notificable','abc']));
please try this and let me know how it works :)
So, I'm trying to make an e-mail view, using data the user posted. The problem is, that specific data is unreachable. I don't know how I'm supposed to get that data.
Here is my controller:
public function PostSignupForm(Request $request)
{
// Make's messages of faults
$messages = [
//removed them to save space
];
//Validation rules
$rules = [
//removed them to save space
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return Redirect::back()->withInput()->withErrors($validator);
}
DB::table('rittensport')->insert([
'note' => $request->get('note'),
//standard instert
]);
/**
* Sending the e-mails to the pilot and co-pilot
*
* #return none
*/
Mail::send('emails.rittensport_signup', $request->all(), function ($message) {
$message->from(env('APP_MAIL'), 'RallyPodium & Reporting');
$message->sender(env('APP_MAIL'), 'RallyPodium & Reporting');
$message->to($request->get('piloot_email'), strtoupper($request->get('piloot_lastname')).' '.$request->get('piloot_firstname'));
$message->to($request->get('navigator_email'), strtoupper($request->get('navigator_lastname')).' '.$request->get('navigator_firstname'));
$message->subject('Uw inschrijving voor de RPR Gapersrit '. date('Y'));
$message->priority(1);//Highest priority (5 is lowest).
});
return Redirect::back();
Well, the view exists and the error I'm facing to is:
Undefined variable: request.
This is how I try to get the data in the e-mail view: {{ $request->get('note') }} I already tried things like {{ $message->note }}, $message['note'] And so on.
Try this:
Mail::send('emails.rittensport_signup', array("request" => $request), function (...