I'm trying to perform a basic Laravel 5 post
request which simply do the following:
- Save some data into a database
- Stores a
file
(image) into public storage under a name which is a possible combo of 20 random numbers following '.jpg' - Save a url (which is used to locate the image in an
img
tag. I'm aware this isn't the best approach so feel free to post)
However, when performing an ordinary post
request with no special headers from angular 6
to laravel 5
, for some reason the File
keeps converting into an empty
array. (Before and after shots are below)
Before:
0: Object { id: 0, body: null, file: File }
After:
0: Object { id: 0, body: null, file: [] }
The File
is full and is used to create a preview on a canvas before submitting so It works. The console is logging this data the line before posting and returning the $req Request
before performing any php code in Laravel.
So the only place anything could get lost in translation is the in between. Code is as follows:
Angular 6:
postToLaravel(object) {
console.log(object.message);
this.http.post(this.url + '/add/message', object).subscribe(res => {
console.log(res);
});
}
Laravel 5:
function addMessage(Request $req) {
return $req->messages;
The empty
array keeps pushing nothing to storage
under a name its registered as a jpg
file but no image is shown.
On the component that images are uploaded we have the following block take place after a (change)="" event:
onFileSelected(e: any) {
this.sectedFile = e.target.files[0];
this.isImage = true;
this.messageService.addImage(this.id, this.sectedFile);
this.preview();
}
The service function just finds the right object and adds a file:
addImage(id, file) {
var index = this.data.findIndex(x => x['id'] == id);
this.data[index]['body'] = null;
this.data[index]['file'] = file;
}
And when the submit button is pressed it just takes all the variables in the service and compacts them into an object. this object contains messages
which is an array of further objects (the objects are as shown at the top) (If that made sense)
pushMessageToServer() {
this.messageService.postToLaravel({
title: this.title,
location: this.location,
gender: this.gender,
age: this.age,
deadline: this.deadline,
messages: this.data
});
}
Is there something I'm missing here?
Let me know should You need any extra info.