I'm trying to make a post request that'll send file name, id
and email
to the database. This is working fine Postman but when I try on the browser. It's giving me ErrorException: Trying to get property 'id' of non-object in file
error.
My steps on the browser:
- Login with user credentials. (successful)
- Trying to upload a file - I get the aforementioned error above. (not successful)
I'm just confused as to why it works in Postman but not on the browser. What am I doing wrong?
Note: The dd($id, $name);
returns the correct values.
front end react code:
fileUpload(file) {
const formData = new FormData()
formData.append('file', file);
formData.append('id', this.state.id);
formData.append('email', this.state.email);
fetch('http://myendpoint/api/auth/wall-of-fame', {
method: 'POST',
body: formData
})
.then(response => console.log(response))
.catch(error => { console.error(error) });
}
controller code:
public function store(Request $request){
$filePath = $request->file('file')->getClientOriginalName();
$id = $request->user()->id;
$email = $request->user()->email;
// dd($id, $email);
$data= [
'file_path' => $filePath,
'user_id' => $id,
'email' => $email
];
DB::table('my.db')->insert($data);
echo "Record inserted successfully.<br/>";
}