Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

unswaa20's avatar

Inertia detect api request 303 in nuxt js and laravel

I have a challenge in the code below how can be able to detect response in my index component approve method from my lender controller so that I could be able to set the loading to false again if the API request is successful which is the status of API request is 303 and the onSuccess in inertia it will detect status 200. What are your insights to solve my problem?

Here is my index component with approve method:

approve(marketLocation) {
  this.loading = true;
  this.$inertia.patch(
    this.$route("admin.marketLocation.approve", { id: marketLocation.id }),
    {
      preserveState: true,
      onSuccess: (response) => {
        console.log("Success:", response);
          this.loading = false;
      },
      onError: (error) => {
        console.log("Error:", error);
        this.loading = false;
      },
    }
  );
},


Here is my lender controller:

   public function approve($id)
    {
        $lenderMarketLocation = $this->fetchLenderMarketLocation($id);
    
        if ($this->canBeApproved($lenderMarketLocation)) {
            $this->approveMarketLocation($lenderMarketLocation);
            $this->sendApprovalNotification($lenderMarketLocation);
            $this->saveLenderMarketLocation($lenderMarketLocation);
        }
    
        $message = 'Market location approved.';

        return redirect()->back()->with('message', $message);
    }
0 likes
7 replies
tykus's avatar

Why does you Controller respond with a Redirect at all? You can return 20x with a JSON payload.

unswaa20's avatar

@tykus Because I will redirect back to another component called show. It will display data of borrower. What is your suggestion about my implementation?

tykus's avatar

@unswaa20 in your approve method you want to handle the Response; if you intend to redirect, then why also do you want to handle the Response yourself? Response statuses for APIs generally are 2xx or 4xx; not 3xx

unswaa20's avatar

@tykus It become 303 because the redirect->back mean it will go back to previous component which is the inde component. The approve method in my index component expect a respond of redirect. My problem because it is a redirect I would not know if the approve method in my lender controller is successful.

unswaa20's avatar

@tykus If i will remove the redirect it would affect the way i fetch the user data

tykus's avatar

@unswaa20 if you really feel that you need the redirect, then don't try to handle the Response.

Please or to participate in this conversation.