Home > Back-end >  How to use laravel redirect back with compact
How to use laravel redirect back with compact

Time:10-06

I am trying to pass value from the price page via input and redirect back to the price page with a value that has been calculated. I am using two functions for this; one is for get price()-PagesController@pricing and the other one is for post feeCal()-PagesController@feeCal.

public function pricing(Request $request)
    {
      return response()->view('pages.price');
    }

public function feeCal(Request $request)
    {
        $value = $request->input('price');
    


        if ($value > 500 or $value > 20000) {
            $fee = $value * 0.015;
        }
        // dd($fee);

        return redirect()->back()->compact('fee');
    }
 <form action="{{ route('page.vfee')}}" method="POST">
                                @csrf
                                <div class="row">
                                    <input type="number" name="price" class="form-control rounded mb-3" placeholder="Type the price here">
                                </div>
                                <div class="row">
                                    <input type="submit" class="btn btn-danger w-100 
                     rounded" value="GET TOTAL FEE">
                                </div>
                            </form>
                            <div class="card-footer">
                                <div class="text-muted">Total fee</div>
                                <div class="text-danger">
                                 {{ $fee ?? '' }}
                                </div>
                                
                            </div>

When you try to input a value it returns a black value but when you dump the fee variable in the controller function you get a result.

CodePudding user response:

Try one of following

 return redirect()->back()->with(compact('fee'));

or

 return redirect()->back()->with('fee', $fee);
  • Related