skip to Main Content

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


  1. Chosen as BEST ANSWER

    Apparently using this code fixed it: 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();
                    formData.append('Acc_id', StagiaireId)
                    formData.append('Message', e.target.elements.message.value)
                    formData.append('StagiaireCV', e.target.elements.CV.files[0])
    
                    axios.post('http://localhost:8000/api/Requestes', formData )
                    .finally(alert('The request has been submitted!'))
                } else {
                    alert("You already sent a request!");
                }
            });
    };
    

    Route::post('/Requestes', function (Request $request) {

    $dataReq = Requests::create([
        'Acc_id' =>  $request->input('Acc_id'),
        'Message' => $request->input('Message'),
        'StagiaireCV' => $request->file('StagiaireCV')->store('cv', 'public'),
        'RequDate' => date('Y-m-d'),
    ]);
    
    return response()->json($dataReq, 200);
    

    });


  2.  Route::post('/Requestes', function (Request $request) {
        
            $dataReq = Requests::create(
                [
                    'Acc_id'      => request('Acc_id'),
                    'Message'     => request('Message'),
                    'StagiaireCV' => $request->file('StagiaireCV')->store('cv'),
                    'RequDate'    => date('Y-m-d'),
                ]
            );
        
            return response()->json($dataReq, 200);
        });
    

    try this

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search