重定向后查看不更新

I'm developing a simple ticket purchasing app using Laravel.

I'm having trouble to update the view after the users have selected the tickets to purchase where doesn't matter whether what type of redirect I used in ajaxRequestPost, the view still stuck at / instead of redirect to payments/{id}.

However, I did get a return of the file payments.index but the view did not update to the returned content.

BitBucket code:https://bitbucket.org/naimrahidin/laravel_ticketing/src/master/

routes/web.php

Route::resource('/', 'TicketController');
Route::post('createOrder', 'OrderController@ajaxRequestPost');
Route::get('/payments/{id}', 'PaymentController@show');

TicketController.php

public function index(){
        $tickets = Ticket::all();

        return view('tickets.index', compact('tickets'));
    }

OrderController.php

public function ajaxRequestPost(Request $request)
{
    $input = $request->all();

    for($i = 1; $i < 4; $i++){
        if($input['ticket_'.$i.'_q'] != 0) {

            $spkAdmin = new Order();
            $spkAdmin->ticket_id = $i;
            $spkAdmin->quantity = (int)$input['ticket_'.$i.'_q'];
            $spkAdmin->total = (int)$input['ticket_'.$i.'_t'];
            $spkAdmin->user_id = $input['user_id'];
            $spkAdmin->save();

            $orders = array(
                'id' => $i,
                'quantity' => (int)$input['ticket_'.$i.'_q'],
                'total' => (int)$input['ticket_'.$i.'_t']
            );
        }
    }

    return redirect()->action(
        'PaymentController@show', ['id' => $input['user_id']]
    );
}

PaymentController.php

public function show($id)
{
    $orders = DB::table('orders')->where('user_id', $id)->get();

    return view('payments.index', ['orders' => $orders]);
}

payments/index.blade.php

@extends('base')

@section('main')
<div class="row">
    <div class="col-sm-12">
        <h1 class="display-3">Payments</h1>
        <h3 class="display-5" data-user-id="1">Naim Rahidin</h3>
        <table class="table table-striped">
            <thead>
                <tr>
                    <td>Ticket ID</td>
                    <td>Quantity</td>
                    <td>Total</td>
                </tr>
            </thead>
            <tbody>
                @foreach($orders as $order)
                <tr>
                    <td>{{$order->ticket_id}}</td>
                    <td>{{$order->quantity}}</td>
                    <td>{{$order->total}}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
        <div class="order_detail">
            <p>Total: RM <span id=grand_total>0</span></p>
            <p id="proceed" class="btn btn-primary">Checkout</p>
        </div>
    </div>
</div>
@endsection

Hi i think $input['user_id'] this id is not a int its a strint please convert to int for using type casting like use (int) start of variable and try this code

return redirect()->action( 'PaymentController@show', ['id' => (int)$input['user_id']] );

checkout.js file

        $.ajax({
            type: 'POST',
            url: '/createOrder',
            data: {
                ticket_1_q: ticket_1_q,
                ticket_2_q: ticket_2_q,
                ticket_3_q: ticket_3_q,
                ticket_1_t: ticket_1_t,
                ticket_2_t: ticket_2_t,
                ticket_3_t: ticket_3_t,
                user_id: 1
            },
            dataType: 'json',
            success: function(data) {
                if (data.status == 200) {
                    window.location.href = data.data;
                }
            },
            error: function(xhr, ajaxOptions, thrownError){
                alert(thrownError);
            },
        });

controller order

    public function ajaxRequestPost(Request $request)
{
    $input = $request->all();

    for($i = 1; $i < 4; $i++){
        if($input['ticket_'.$i.'_q'] != 0) {

            $spkAdmin = new Order();
            $spkAdmin->ticket_id = $i;
            $spkAdmin->quantity = (int)$input['ticket_'.$i.'_q'];
            $spkAdmin->total = (int)$input['ticket_'.$i.'_t'];
            $spkAdmin->user_id = $input['user_id'];
            $spkAdmin->save();

            $orders = array(
                'id' => $i,
                'quantity' => (int)$input['ticket_'.$i.'_q'],
                'total' => (int)$input['ticket_'.$i.'_t']
            );
        }
    }
        $url = "/payments/".$input['user_id'];
        return response()->json([
            'status'=>200,
            'message'=>"done",
            'data'=> $url
        ]);
}

Figured out the answer for this.

Since I'm calling createOrder via Ajax, OrderController should send the rendered view as response, and at ajax.success should replace the html.

added in OrderController.php

$html = view('payments.index')->render();
$response = response()->json(['success' => true, 'html' => $html]);
return $response;

ajax

success: function(data) {
    $('.container').html(data.html);
}