0

I am new to laravel so this particular error is giving me headache. I am trying use a datatable in laravel vue.js app. I have the following code in my laravel backend and when I try retrieving the data from the data, I get the Undefined array key error message in the dev tools. How do I resolve the issue please? Your help would be greatly appreciated.

public function getAccommodations(Request $request)
{
    if ($request->input('showdata')) {
        return Accommodation::orderBy('id', 'desc')->get();
    }        

    $columns = ['name','town_city','district','email','phone'];

    $length = $request->input('length');
    $column = $request->input('column');

    $search_input = $request->input('search');

    $query = Accommodation::select('name','town_city','district','email','phone')->orderBy($columns [$column]);

    if ($search_input) {

        $query->where(function($query) use ($search_input) {

            $query->where('name', 'like', '%'. $search_input . '%')
            ->orWhere('town_city', 'like', '%'. $search_input . '%')
            ->orWhere('district', 'like', '%'. $search_input . '%')
            ->orWhere('email', 'like', '%'. $search_input . '%')
            ->orWhere('phone', 'like', '%'. $search_input . '%')

        });

    }

    $accommodations = $query->paginate($length);
    return ['data' => $accommodations];
}

Below is the stacktrace

[2022-04-24 20:00:17] local.ERROR: Undefined array key "" {"userId":2,"exception":"[object] (ErrorException(code: 0): Undefined array key \"\" at C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\app\\Http\\Controllers\\AccommodationsController.php:50)
[stacktrace]
#0 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\HandleExceptions.php(231): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Undefined array...', 'C:\\\\Users\\\\Nat Os...', 50)
#1 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\app\\Http\\Controllers\\AccommodationsController.php(50): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}(2, 'Undefined array...', 'C:\\\\Users\\\\Nat Os...', 50)
#2 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): App\\Http\\Controllers\\AccommodationsController->getAccommodations(Object(Illuminate\\Http\\Request))
#3 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('getAccommodatio...', Array)
#4 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(261): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\AccommodationsController), 'getAccommodatio...')
#5 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(204): Illuminate\\Routing\\Route->runController()
#6 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(725): Illuminate\\Routing\\Route->run()
#7 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(141): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#8 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#9 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(180): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#10 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#12 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#13 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(180): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#14 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#15 C:\\Users\\Nat Osei\\Desktop\\projects\\gh_diary\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest(Object(Illuminate\\Http\\Request), Object(Illuminate\\Session\\Store), Object(Closure))

16
  • Why are you passing an array to the second parameter of orderBy? Commented Apr 24, 2022 at 19:59
  • To be able to use the columns in my vue (frontend) please. Commented Apr 24, 2022 at 20:05
  • 1
    It seems that $column is an empty string. Perhaps, check what data you're sending and add default fallback; $column = $request->input('column', 'name') Commented Apr 24, 2022 at 20:06
  • I just reformatted the code and it looks odd that the $query has a closure then another $query nested inside of it. It will never leave the scope of that function, that's probably why no keys are being returned. Commented Apr 24, 2022 at 20:08
  • @JacobMulquin I assume it's to isolate the ->orWhere from any other (potential) ->where statement. It doesn't need to returns something has it's a mutable query Commented Apr 24, 2022 at 20:11

1 Answer 1

1

The issue is that you're trying to get an element from your array $columns with an empty string.

$columns = ['name','town_city','district','email','phone'];

$column = $request->input('column'); // empty string

$query->orderBy($columns[$column]);

You can fix the issue by setting a default value

$column = $request->input('column', 0); 
$query->orderBy($columns[$column]); // $columns[0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.