Home > Blockchain >  Undefined Array Key ""
Undefined Array Key ""

Time:04-25

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))

CodePudding user response:

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]
  • Related