How to pass array in Mail Function in Laravel 5.7 - laravel

Below is my mail function:
How to can I pass an array to mail function?
public function mail(Request $request , $id) {
$data=[
'owner'=>MyRoom::where('id',$id)->get(),
'data2'=>$request->all(),
];
Mail::send('emails.mail' , $data, function($message) use ($data){
$message->to($owner->created_by->email , $owner->created_by->name)
->subject('Room showing Request From OpenRoomList');
$message->from('regmibipin13#gmail.com','OpenRoomList');
});
echo "Email Send check your inbox";
}

Firstly instead of doing get which will give you a collection, do :
'owner' => MyRoom::find($id);
In your resources/emails/mail.blade.php blade file you can directly use $owner and $data2 variables using blde syntax like : {{ $owner->somecolumn }} and {{ $data2['somefield'] }}

Related

How to pass the custom variables from ResetPasswordController to reset blade

How to pass the custom variables from ResetPasswordController to reset blade template.
ResetPasswordController.php
public function showResetForm(Request $request, $token = null)
{
$data = array(
'title'=>'Reset password',
'description'=> 'Reset password to abc.com',
'seo_keywords'=> 'Reset password to abc.com',
);
return view('auth/password/reset',$data);
}
By returning a view(), the second argument can be used to pass variables to the blade template (just like you have done)
public function showResetForm(Request $request, $token = null)
{
return view('auth/password/reset',[
'title' =>'Reset password',
'description' => 'Reset password to abc.com',
'seo_keywords' => 'Reset password to abc.com',
]);
}
These would then be accessable as {{ $title }}, {{ $description}}, {{ $seo_keywords}}.
If you are unable to retrieve these, it may be because you are editting the wrong blade template. The default template is located at auth.passwords.reset (resources/views/auth/passwords/reset.blade.php).
I'd suggest just adding a {{ dd('here) }} at the top of that template to make sure it is in fact the template being used by your application!

Why giving undefined variable in sending Email attachment in Laravel?

I can't pass any variable to Email body from Controller. I have searched many solutions but no is matching with me.
I am using Mailable. But it is empty. I am not sure it is creating problem or not.
public function __construct(){}
public function build(){}
I have tested with dummy Email without passing variable. Email is sending successfully. So, I think there is no problem with configuration.
Function in controller:
public function mail()
{
$info = Invoice::find(65)->first();
// 65 is giving me value. I have checked with dd()
$data = [
'invoice' => $info->invoiceNumber
];
Mail::send(['text'=>'invoice.test'], $data, function($message) use($data){
$pdf = PDF::loadView('invoice.test');
$message->to('example#gmail.com','John Smith')->subject('Send Mail from Laravel');
$message->from('from#gmail.com','The Sender');
$message->attachData($pdf->output(), 'Invoice.pdf');
});
return 'Email was sent';
}
test.blade.php
<h1>It is working!! {{ $invoice }}</h1>
Data sending style is followed by:
Laravel Mail::send how to pass data to mail View
Try adding the $data with the PDF view:
$pdf = PDF::loadView('invoice.test', $data);
But if you want to add an email body content different with the PDF content, try this:
Mail::send([], $data, function($message) use($data){
$pdf = PDF::loadView('invoice.test', $data);
$message->to('example#gmail.com','John Smith')->subject('Send Mail from Laravel');
$message->from('from#gmail.com','The Sender');
$message->setBody('sample mail content');
$message->attachData($pdf->output(), 'Invoice.pdf');
});

Laravel Cannot Decrypt Encrypted ID On Controller

I cannot decrypt the encrypted value on a controller after clicking on the submit button on my blade file below.
Controller :
public function edit($id)
{
$encrypted_id = encrypt($id);
return view('my.blade.edit', compact('encrypted_id'));
}
public function update(Request $request, $id)
{
$decrypted_id = decrypt($id);
dd($decrypted_id);
}
Blade: (my.blade.edit)
{{ Form::open(['route' => ['route.update', $encrypted_id ], 'method' => 'PATCH']) }}
{{ Form::button('Update', ['type' => 'submit', 'name' => 'update']) }}
{{ Form::close() }}
I am expecting an integer value on my dd(); but I still getting an encrypted string.
Well, as I've already written in the comments, first and simple is to check expected output and exact output.
So far we discovered, that value was sent to view isn't equal to value received in update() method.
id was encrypted twice, but we don't see two encrypt() calls in the code from the question. Probably some other code layer was making that.

Laravel 5.5 - Save multiple data continiously from blade

I want to make a PHP method using laravel. I want to do the comparison of criteria and criteria. Here is the controller code :
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
return view('kriteria_kriterias.create')->with('kriteria1', $kriteria1)->with('kriteria2', $kriteria2)->with('data', $data);
}
and this is the blade code :
It will make the form appear as total of criteria#
The problem is, I can't save it all to database. How do I get it to do this?
Updated method in the controller to the following:
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
How to output in the blade file:
{{ $kriteria1 }}
{{ $kriteria2 }}
Or you update the controller to pass the complete results:
public function create($id1, $id2)
{
$kriteria1 = Model\Kriteria::find($id1);
$kriteria2 = Model\Kriteria::find($id2);
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
And the in the blade you can accss the data in various ways, one way is a foreach loop using blade in the blade template:
#foreach($kriteria1 as $k1)
{{ $k1 }}
#endforeach
#foreach($kriteria2 as $k2)
{{ $k2 }}
#endforeach'
To accept multiple values dynamicaly in the controller you can try something like this:
public function create($ids)
{
$results = collect([]);
foreach($ids as $id) {
$kriteria = Model\Kriteria::findOrFail($id);
if($kriteria) {
$results->put('kriteria' . $id, $kriteria);
}
}
return view('kriteria_kriterias.create')->with($results);
}
Then use the same looping method mentioned above to display them in the blade or a for loop that gets the count and displays accordingly.
maybe you forgot to add the opening tag ;)
{!! Form::open(array('url' => 'foo/bar')) !!}
//put your code in here (line 1-34)
{!! Form::close() !!}

Undefined variable inside Laravel view when using Mail

While trying to send verification email using Laravel 5.2, I get an error:
Undefined variable: confirmation_code (View:
C:\xampp\htdocs\laravel\resources\views\email\verify.blade.php)
My code looks like this:
Controller.php:
public function postSignup(Request $request){
$this->validate($request,[
'email'=>'required|unique:users|email',
'name'=>'required|max:50|min:3',
'password'=>'required|min:6',
'con-password'=>'required|same:password',
]);
$confirmation_code=['code'=>str_random(20)];
$name = $request->input('name');
Mail::send('email.verify',$confirmation_code,function($message)
use($request,$name){
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
User::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password'))
]);
return redirect()->back()->with('info','Congratulation you have been successfully registered.Please check your email for verification');
}
Mail.verify.blade.php:
<h2>Verify Your Email Address</h2>
<div>
Thanks for creating an account with the verification demo app.
Please follow the link below to verify your email address
{{ URL::to('register/verify/'.$confirmation_code) }}.<br/>
</div>
</body>
Try this:
Mail::send('email.verify', compact('confirmation_code'), function ($message) use($request, $name) {
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
The reason why it fails is that Laravel views accept an associative array as their data, so that it can turn them into variables using keys as variables names and match them to their corresponding values.
What compact does is turn your variable into an associative array, with the name of the variable as its key (sort of the opposite of what the Laravel view will do).

Resources