React:
function SendRequest(e) {
e.preventDefault();
axios.get(`http://localhost:8000/api/Stagires/Requestes/Finder/${StagiaireId}`)
.then(data => {
if (!data.data.success) {
const formData = new FormData(e.target);
const Message = formData.get('message')
const StagiaireCV = formData.get('CV')
console.log(Message, StagiaireCV);
axios.post('http://localhost:8000/api/Requestes',{'Acc_id':StagiaireId, 'Message': Message, 'StagiaireCV': StagiaireCV} )
} else {
alert("You already sent a request!");
}
});
};
React – form:
<form onSubmit={SendRequest}>
<div>
<label htmlFor="message">Ton Message</label>
<textarea id='message' placeholder='Message' name='message'></textarea>
</div>
<div onClick={AddCv}>
<input ref={file} id="file" type="file" name="CV" style={{ display: 'none' }}/>
<div>
<span className="material-symbols-outlined">download</span>
<p>Ajouter votre CV</p>
</div>
</div>
<div>
<button type="submit" >Envoyer la demande</button>
</div>
</form>
And for laravel:
First api:
Route::get('/Stagires/Requestes/Finder/{id}', function ($id) {
$data = Stagiaire_acc::find($id);
$Requests = $data->Requests;
if ($Requests) {
return response()->json(['success' => true]);
}
else{
return response()->json(['success' => false]);
};
});
And the second api:
Route::post('/Requestes', function (Request $request) {
$dataReq = Requests::create(
[
'Acc_id' => request('Acc_id'),
'Message' => request('Message'),
'StagiaireCV' => request('StagiaireCV')->file()->store('cv'),
'RequDate' => date('Y-m-d'),
]
);
return response()->json($dataReq, 200);
});
When ever I submit the the file it keep responding with:
"Call to a member function file() on array"
I tried my best and couldn’t find a solution, hopefully someone can help me out.
2
Answers
Apparently using this code fixed it: function SendRequest(e) { e.preventDefault()
Route::post('/Requestes', function (Request $request) {
});
try this