AlenV

Laravel 5.5 Ajax problem

Posted 8 months ago by AlenV

Hello,

The problem is that I want to sort CarFeatures by name, but when I change listing from A-Z to Z-A nothing happens, I see the request in developer tools, but no changes.

Route

    Route::get('/carfeature/{sortBy}', ['uses' => '[email protected]']);

CarFeatureController

public function listCarFeatures(Request $request, $sortBy) {
        if($request->has('sortBy')){
            $sortBy = $request->input('sortBy');

        }
        if($sortBy == 'az') {
            // Get car features from Database
            $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'asc')->paginate(5);
        } else {
            $carFeatures = DB::table('car_features')->orderBy('car_feature_name', 'desc')->paginate(5);
        }

        // Return view with CarFeatures
        return view('private.carfeature.carfeature_show')
        ->with(['carFeatures' => $carFeatures]);
    }

AJAX

function sortCarFeatures() {
    x = document.getElementById('carFeatureSort').value;

    $.ajax({
        url: 'carfeature',
        type: 'GET',
        data: { sortBy: x },
        success: function(response){
            console.log('Success: ' + response);
        },
        error: function(response){
            console.log('Error: ' + response);
        }
    });

}

HTML

<!-- Sort By -->
    <div class="col-md-6 d-flex justify-content-right">

        <div class="col-md-5 mt-3">
            <h4 class="text-center">Sort by</h4>
        </div>

        <div class="col-md-7 mt-3">
            <select id="carFeatureSort" class="form-control" onchange="sortCarFeatures()">
                <option selected value="az">
                    Name A-Z
                </option>
                <option value="za">
                    Name Z-A
                </option>
            </select>
        </div>
    </div>

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.