Home » Php » php – Route not defined Sort (laravel)

php – Route not defined Sort (laravel)

Posted by: admin February 25, 2020 Leave a comment

Questions:

Ok I’ve designed some functions to sort my data up and down. For some reason it only works sorting down (desc) and I get an error saying
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [contacts.sortUp] not defined.

Not sure what I’m missing

here are the routes

// Blade Routes
Route::get('/search', '[email protected]')->name('contacts.search');
Route::get('/postSearch', '[email protected]')->name('contacts.postSearch');
Route::get('/sort/{field}/{currentField}/{dir}', '[email protected]')->name('contacts.sort');
Route::get('/sort/{field}', '[email protected]')->name('contacts.sortUp');
Route::get('/sort/{field}', '[email protected]')->name('contacts.sortDown');
Route::get('/details/{column}', '[email protected]')->name('addresses.details');

and the controller functions

    public function sort($field, $currentField , $dir)
    {
        $validFields = Contact::getFields();

        if( in_array($field, $validFields) && in_array($currentField, $validFields) && in_array($dir, ['asc','desc']) ) {

            if ($field !== $currentField) 
            {
                return redirect()->route('contacts.sortUp', ['field' => $field]);
            }
            else if ($dir === 'asc')
            {
                return redirect()->route('contacts.sortDown', ['field' => $field]);
            }
            else if ($dir === 'desc')
            {
                return redirect()->route('contacts.sortUp', ['field' => $field]);
            }
            else {
                print("Error: Invalid field and/or direction to sort by");
            }
        }
    }

    public function sortUp ($field)
    {
        $data = Contact::orderBy($field, 'asc')->paginate(10);
        $currentField = $field;
        $dir = 'asc';
        return view('contacts.sort', ['contacts' => $data, 'field' => $field, 'currentField' => $currentField, 'dir' => $dir]);
    }

    public function sortDown ($field)
    {
        $data = Contact::orderBy($field, 'desc')->paginate(10);
        $currentField = $field;
        $dir = 'desc';
        return view('contacts.sort', ['contacts' => $data, 'field' => $field, 'currentField' => $currentField, 'dir' => $dir]);
    }

Not exactly sure what’s going wrong. I’m sure I’m missing something simple

How to&Answers: