php - Laravel & angular Get image from Request
Get the solution ↓↓↓I have an angular form that contain many fields and one of this fields is image.when I am sending this data as Object Request to backend(laravel) give me error 500 because cant find the image(file).
I tried it without any data just the image(file) its worked with this code:
public function insertFile(Request $request){
$file = $request->file('file');
$originalImage = $file->getClientOriginalName();
$path = $file->move(public_path('images'), $originalImage);
}
Now I am trying to get the image(file) from CompanyDetails Object field image
TypeScript object
export class CompanyDetails {
user_id?:any;
companyName: string;
managerName: string;
image?: any;
}
this my object after assign data in object
private buildForm(){
this.detailsModel.companyName = this.myForm.companyDetails.value.companyName;
this.detailsModel.managerName = this.myForm.companyDetails.value.managerName;
const formData = new FormData();
formData.append('file' , this.selectedFile,this.selectedFile.name);
this.detailsModel.image = formData;
}
this is my php (laravel) code
public function insertDetails(Request $request){
$data = $request->input();
$managerName= $data['managerName'];
$description= $data['description'];
//get image from request
$image= $data['image'];
$file = $image->hasFile('file');
$originalImage = $file->getClientOriginalName();
$imagePath = 'public/uploads/'.$originalImage;
$imageName = $originalImage;
$file->move(public_path('images'), $originalImage)
}
Answer
Solution:
I am working with reactive forms. As you can see here I use the toFormData() method before sending it to the backend.
I don't know if it helps you but let me know.
export class ContactFormularComponent implements OnInit {
contactForm: FormGroup;
mailAddresses = environment.contactMailAddresses;
constructor(
private contactService: ContactService,
private _snackBar: MatSnackBar) {}
ngOnInit() {
this.initForm();
}
initForm() {
this.contactForm = new FormGroup({
email: new FormControl(null, [Validators.required, Validators.email]),
name: new FormControl(null),
subject: new FormControl(null),
message: new FormControl(null),
attachement: new FormControl(null),
receiver: new FormControl(null, [Validators.required])
});
}
send() {
if (this.contactForm.valid) {
var data = toFormData(this.contactForm.value);
this.contactService.sendMail(data).subscribe((response) => {
this._snackBar.open(response['message'], 'Ok', {
duration: 5000,
panelClass: 'snackbar-success'
});
},
(response)=>{
this._snackBar.open(response['message'], 'Ok', {
duration: 5000,
panelClass: 'snackbar-success'
});
});
this.initForm();
}
}
}
export function toFormData<T>( formValue: T ) {
const formData = new FormData();
for ( const key of Object.keys(formValue) ) {
const value = formValue[key];
formData.append(key, value);
}
return formData;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
React
React is currently the leader in JavaScript UI frameworks. First, the Facebook developers started working on this to make their job easier. An app called Facebook Ads grew very quickly, which meant complex management and support. As a result, the team began to create a structure that would help them with efficiency. They had an early prototype before 2011, and two years later, the framework was open source and available to the public. It is currently used by many business giants: AirBNB, PayPal, Netflix, etc.
https://reactjs.org/
Angular
Angular is one of the most powerful JavaScript frameworks out there. Google uses this platform to develop a Single Page Application (SPA). This development environment is known primarily because it provides developers with a better environment for combining JavaScript with HTML and CSS. More than half a million sites like google.com, youtube.com, etc. use Angular.
https://angular.io/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.