Yassine Ghz
Yassine Ghz

Reputation: 77

Solved : Laravel Controller not redirecting properly after an axios post

im trying to redirect after storing some data passed using axios(ReactJS). the controller is working fine it store data in the DB ,but it wont redirect to the destination i want.

my component code that s work perfectly:

    async onSubmitButton(e) {
    e.preventDefault();
    const formData = new FormData();
    formData.append("objet", this.state.objet);
    formData.append("id_p", this.state.id_p);
    const response = await axios.post("/CreateCDM", formData, {
    headers: {
    "Content-Type": "multipart/form-data"
    }
    })
    .catch(err => { 
        console.log(err);
        this.setState({errorMessage: err.message});    
    })
}

and this is my controller :

public function store(Request $request)
{
    $dm = new DossierMedical;
    $id = Auth::user()->id;

    $dm->objet = $request->objet;
    $dm->analyseRequis = 0;
    $dm->medcine = $id;
    $dm->patient = $request->id_p;

    $dm->save(); //Working , i cant see that in the DB

    $specialite = DB::table('specialites')
        ->where('nom', 'not like', 'Radiologie')
        ->where('nom', 'not like', 'Laboratoir d\'Analyse')
        ->SELECT('*')
        ->GET();

    return view('dossierMedicale.patient.dm' ,
    ['objet' => $request->objet ,
    'id' => $dm->id ,
    'Specialite' => $specialite
    ]);

}

the problem is that im not redirecting to the view i want ,and i cant see any server error in the console.

PS : if i post the data using blade.php(without Reactjs) it redirect me

Upvotes: 0

Views: 2226

Answers (1)

YingHua Chai
YingHua Chai

Reputation: 75

The redirection should be done in ReactJS instead of Laravel's controller.

Sample in ReactJS:

    onSubmitButton(e) {
      e.preventDefault();
      const formData = new FormData();
      formData.append("objet", this.state.objet);
      formData.append("id_p", this.state.id_p);

      axios.post("/CreateCDM", formData, {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      })
      .then(response => {
         // obtain the data return from controller 
         const { objet, id, Specialite } = response.data;

         //perform your redirection to other routes.
         window.location.href = "/other-routes/";
      })
      .catch(err => { 
        console.log(err);
        this.setState({errorMessage: err.message});    
      })
    }

Sample in Controller:

public function store(Request $request)
{
    $dm = new DossierMedical;
    $id = Auth::user()->id;

    $dm->objet = $request->objet;
    $dm->analyseRequis = 0;
    $dm->medcine = $id;
    $dm->patient = $request->id_p;

    $dm->save(); //Working , i cant see that in the DB

    $specialite = DB::table('specialites')
        ->where('nom', 'not like', 'Radiologie')
        ->where('nom', 'not like', 'Laboratoir d\'Analyse')
        ->SELECT('*')
        ->GET();

    return response()->json([
      'objet' => $request->objet ,
      'id' => $dm->id ,
      'Specialite' => $specialite
    ], 200); 
}

Upvotes: 1

Related Questions