I'm submitting a contact form using Angular and Laravel. In the form, there is some checkboxes. User can select multiple options (services) and the request will go to Laravel controller through API. In console.log, the request is working fine but when I'm trying to access services in Laravel, I'm getting this error in console:
Error: "Array to string conversion"
Angular Component:
export class ContactComponent {
contactForm!: FormGroup;
successMsg: boolean = false;
warningMsg: boolean = false;
public isSaving = false;
Data: Array<any> = [
{name: 'floor', value: 'Floor Maintenance'},
{name: 'deep', value: 'Deep Cleaning'},
{name: 'residential', value: 'Residential Cleaning'},
{name: 'commercial', value: 'Commercial Cleaning'},
{name: 'industrial', value: 'Industrial Cleaning'},
{name: 'sanitization', value: 'Sanitization and Disinfection'}
];
ngOnInit(): void {
this.contactForm = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]],
email: ['', [Validators.required, Validators.minLength(3)]],
phone: ['', [Validators.required, Validators.minLength(3)]],
message: ['', [Validators.required, Validators.minLength(3)]],
services: this.fb.array([], [Validators.required]),
})
}
onCheckboxChange(e: any) {
const services: FormArray = this.contactForm.get('services') as FormArray;
if (e.target.checked) {
services.push(new FormControl(e.target.value));
} else {
let i: number = 0;
services.controls.forEach((item: any) => {
if (item.value == e.target.value) {
services.removeAt(i);
return;
}
i ;
});
}
}
onSubmit() {
this.isSaving = true;
this._emailService.contact(this.contactForm.value)
.subscribe(
response => {
console.log('Success!', response);
},
error => {
console.error('Error.', error);
}
)
}
}
Laravel Controller
public function submit(Request $request) {
Mail::to($request->email)->send(new Quotation($request));
return response()->json($request->all, 200);
}
Quotation.php
public function build(Request $request)
{
return $this->markdown('emails.angular')->with([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'message' => $request->message,
'services' => $request->services
]);
}
email.blade.php
@component('mail::message')
# A New Quotation Received
@component('mail::table')
| Details: | | |
| :------------- |:-------------:| --------------:|
| Email | | {{ $email }} |
| Name | | {{ $name }} |
| Phone | | {{ $phone }} |
| Message | | {{ $message }} |
| Services | | {{ $services}} |
@endcomponent
It only happens when I try to send the email. If I access services in Controller like this return response()->json($request->services)
, I get this response in console.
['Floor Maintenance', 'Deep Cleaning']
But unable to send the email
CodePudding user response:
It looks like you are trying to access the services field in your form data as an array, but it is being treated as a string. This can happen if you are trying to access the services field directly, like $request->services, instead of using the appropriate method to retrieve input from the request.
In Laravel, you can use the input method to retrieve form data from a request. For example, to get the value of the services field, you can use $request->input('services'). This will return an array of the selected values.
You can also use the json method to retrieve the request data as a JSON object. This can be useful if you are sending a JSON payload in the request body. For example, you can use $request->json('services') to get the value of the services field.
You can try something like this
public function submit(Request $request)
{
$services = $request->input('services');
Mail::to($request->input('email'))->send(new Quotation($request));
return response()->json($request->all(), 200);
}