Laravel mail blade passing a variable - laravel

I need to pass some variables to my sending email.
Here is my code:
$data = [
'first' => $request->first,
'last' => $request->last,
'business_org' => $request->business_org,
'instagram' => $request->instagram,
'email' => $request->email,
'phone' => $request->phone,
'unique' => $request->unique,
'purchased' => $request->products_purchased,
'city' => $request->city,
'state' => $request->state,
'filename' => $fileName
];
// send email with details
Mail::send('emails.justshoot', $data, function($message) {
$message->from('us#something.com', 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('myemail#gmail.com');
});
I then attempt to access the variable so I can display it in my email.
emails.justshoot.blade.php
{{$data}} gives an error. What am I doing wrong?

You are totally fine with what you are doing, but the second param is the data to pass to the view, and as with the with([]) method to call the view, the array passed will generate an object for each entry, and so with what you are doing you are generating $first, $last, $business_org and the $data is just the name of the array, so it isn't been passed as element to the view: if you want this, you should pass [$data] to the mail send :
$data = [
'first' => $request->first,
'last' => $request->last,
'business_org' => $request->business_org,
'instagram' => $request->instagram,
'email' => $request->email,
'phone' => $request->phone,
'unique' => $request->unique,
'purchased' => $request->products_purchased,
'city' => $request->city,
'state' => $request->state,
'filename' => $fileName
];
// send email with details
Mail::send('emails.justshoot', [$data], function($message) {
$message->from('us#something.com', 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('myemail#gmail.com');
});
and then in the view you can do {{$data}}
TIPS: You should create a mail with php artisan and then you are able to do whatever you want, in a more elegant way

Related

How is it possible to retrieve the id of a Model right after it was created in the same controller function?

Let me show you my code, and place comments for you guys to better understand:
$homework = new Homework([ // I create Homework (And I indeed want to get the ID of the one that was just created).
'subject_id' => $request->subject_id,
'user_id' => auth()->user()->id,
'title' => $request->name,
'image' => $path,
'progress' => $request->progress,
'description' => $request->description,
'duedate' => $request->date
]);
$homework->save(); // I save it
$homeworkid = Homework::where('id', $id)->first(); // I try to retrieve it, but I'm not sure how to get it as I need to define `$id`.
$progress = newProgress([
'user_id' => auth()->user()->id,
'homework_id' => $homeworkid, // I need this for the relationship to work.
'title' => 'Initial Progress',
'description' => 'This progress is auto-generated when you create an assignment',
'username' => auth()->user()->name,
'progress' => $homeworkid->progress
]);
$progress->save(); // I save the progress
Well, as you saw, I'm trying to retrieve the ID of Homework right after it was created, but I'm not sure how to define $id in order to get it.
There is no need to instantiate a new model and saving it if your are not doing anything between instantiating and saving, you can use the create method instead:
$homework = Homework::create([
'subject_id' => $request->subject_id,
'user_id' => auth()->user()->id,
'title' => $request->name,
'image' => $path,
'progress' => $request->progress,
'description' => $request->description,
'duedate' => $request->date
]);
$homework->id; // get the id
After saving / creating the model you can access the id like you normally would:
$homework->id
What you then could do is setup the relationships between your models so you can do the following after creating a new homework:
$homework->newProgress()->create([
'user_id' => auth()->user()->id,
'title' => 'Initial Progress',
'description' => 'This progress is auto-generated when you create an assignment',
'username' => auth()->user()->name,
'progress' => $homework->progress
]);
This way you don't have to pass the homework id when creating a new newProgress, laravel will pass it automatically for you.
This is very simple for you. No need to complex it.
$homework->save(); // I save it
After this line just use only
$progress = newProgress([
'user_id' => auth()->user()->id,
'homework_id' => $homework->id, // I need this for the relationship to work.
'title' => 'Initial Progress',
'description' => 'This progress is auto-generated when you create an assignment',
'username' => auth()->user()->name,
'progress' => $homework->progress
]);
You don't no need this line of code
$homeworkid = Homework::where('id', $id)->first(); // I try to retrieve it, but I'm not sure how to get it as I need to define `$id`.
$data = $homework->save();
Get the ID this way: $data->id

Laravel sending attachment from two different inputs

I'm trying to send an email through Laravel from a form that has two different file inputs with different names, cv and cover_letter.
The build function works just fine but it only attaches one file when it sends the email, I'd like it to send both.
I've tried this solution here on SO but no luck sending both attachments either.
public function build(Request $request)
{
$today = Carbon::now()->format('Y-m-d');
return $this->from([
'email' => $request->email,
'name' => $request->name
])
->to( 'jobs#domain.com' )
->subject( New job application '.$request->name.' for the position of '.$request->role.'.')
->view('emails.jobsform')
->with([
'name' => $request->name,
'tel' => $request->tel,
'email' => $request->email,
'role' => $request->role,
'location' => $request->location,
'call_code' => $request->call_code,
])
->attach(
$request->cv, [
'as' => $today.".".$request->name.".".$request->role.'.pdf',
'mime' => 'application/pdf'],
$request->cover_letter, [
'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf',
'mime' => 'application/pdf']
);
}
Hope someone can help, thanks.
In example, he use attach with foreach(), it means that for new file he has new attach, and u want to attach all files in one attach(). Try this
->attach(
$request->cv, [
'as' => $today.".".$request->name.".".$request->role.'.pdf',
'mime' => 'application/pdf'])
->attach(
$request->cover_letter, [
'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf',
'mime' => 'application/pdf']
);

Laravel - Validate value against string

I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);

Laravel 5.2 PHPUnit attach() doesn't move file

I have this method to store new user
public function register(CreateUserRequest $request){
$file = $request->file('idcard');
$fileName = rand(0, 99999).$file->getClientOriginalName();
if($request->hasFile('idcard') && $request->file('idcard')->isValid()){
$request->file('idcard')->move("images/idcard/", $fileName);
}
User::create([
'role_id' => 1,
'email' => $request->email,
'password' => $request->password,
'full_name' => $request->full_name,
'address' => $request->address,
'phone' => $request->phone,
'family_name' => $request->family_name,
'family_address' => $request->family_address,
'family_phone' => $request->family_phone,
'idcard' => $fileName,
'status' => 'unconfirmed',
'balance' => 0,
]);
Session::flash('success', 'Please check your email to activate your account.');
return redirect('/register');
}
And I have this unit test
public function testNewUserRegistration()
{
$this->visit('/register')
->type('This is full name', 'full_name')
->type('JL. Mulyorejo 226 D', 'address')
->type('085788884877', 'phone')
->type('Rizky Sugiarto', 'family_name')
->type('JL. Kandangan', 'family_address')
->type('085766669999', 'family_phone')
->attach('/var/www/html/autodealer/images/pics/banner_car.jpg', 'idcard')
->type('mail#yahoo.co.id', 'email')
->type('123456789', 'password')
->type('123456789', 'password_confirmation')
->press('Submit')
->seeInDatabase('users', ['email' => 'mail#yahoo.co.id', 'role_id' => 1, 'balance' => 0, 'status' => 'unconfirmed'])
->seePageIs('/register')
->see('Please check your email to activate your account.');
}
If I test my function via browser, it successfuly input database and move file.
But when I test via PHPUnit it pass the test, successfully input database but the image doesn't move.
Is there something wrong with my attach() or something else wrong?
Thanks, any help appreciated.

how to print value in controller in codeigniter

am using code as below but am not getting any value.i gave as print_r($this->title); what i have to do to get the value. thanks
if(isset($_POST['is_ajax']) && $_POST['is_ajax']) {
print_r($this->title);
$respondentArray = array(
'state' => $_POST['state'],
'name' => $_POST['name'],
'title' => $_POST['title'],
'dline' => $_POST['directline'],
'email' => $_POST['email'],
'organization' => $_POST['organization'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'state1' => $_POST['state1'],
'zip' => $_POST['zip'],
'generalphone' => $_POST['generalphone'],
'fax' => $_POST['fax'],
);
$this->session->set_userdata($respondentArray);
i guess this is what you need
echo $this->input->post('title') ;
$this->input->post(postedValues) this gets all the input values that is posted...
Use var_dump($this->title); to test

Resources