Laravel Error Message : Undefined index : column

Posted on

Another article is written and it is intended specifically to discuss how to solve an error which is shown as in the title of this article, ‘Undefined index : column’. And the column which is mentioned in this article is referring to the column name of a table.

The table is mapped by a model file which is going to be set further in a controller file specifically in a method. The following is the explanation on how the condition is really happened for the error :

1. There is a table named ‘server’. In this context, the table has already been created and it can be accessed.

2. It is successfully mapped or it is being manipulated using a model file named ‘Server’. Below is the content of the model file named ‘Server’ :

class Server extends Model
{
    //
    protected $table = 'server';
    ...
}

The above model file can be created by using the following command executed in a bash prompt. It is directly executed inside the root folder of the web-based application powered by Laravel framework as shown in the following line :

user@hostname:/var/www/html/testing/laravel-project$ php artisan make:model Server
Model created successfully.
user@hostname:/var/www/html/testing/laravel-project$

3. To be able to add a new record in the associated table which is represented by a single file model, it must be instantiated first in a controller file. In this context, the name of the controller file is ‘ServerController.php’. Below is the snippet code given :

class ServerController extends Controller {
public function checkGeneral(Request $request) 
{
      $server = new Server();
      $server->label = $_POST["label"];

To retrieve any input or data inserted by user in a form made in a blade view template file, the above line of code is trying to retrieve it from a input type field which has a name of “sn”. It is an abbreviation of serial number or it is used to be associated with the serial number which is possessed by a server. The value of it will then stored into the property or attribute possessed by an instantiated object of server which is associated with the table. So, the property or attribute named ‘serial_number’ is actually referred to the name of the column where the table is associated with this object.

The above snippet code is actually generating error as follows :

ErrorException in /var/www/html/testing/laravel-project/app/Http/Controllers/ServerController.php line 192 :Undefined index: sn

Apparently, the error stated that in the line :

$server->serial_number = $_POST["sn"]

Laravel claimed that there is no index for ‘sn’, it is because when submitting the POST request, although it is a POST request, to be able to retrieve the value from an input text field, to resolve the above error, below is the definition on how to acquire it :

$server->serial_number = $request["sn"]

This is the form which is defined in the blade view template file :

{{ Form::open(array('url' => 'server/check_general', 'class'=> 'form-horizontal'))}}
...
<div class="form-group">
<label class="col-md-2 control-label">Serial Number</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="Serial Number" name="sn"
value="">
</div>
</div>
...
{{ Form::close()}}

We have also defined a route for the above url target in the routes/web.php file :

Route::post('/server/check_general','ServerController@checkGeneral');

Basically, the error can be handled just by defining another method to retrieve the data or information retrieved from a text field. From the following line :

$server->serial_number = $_POST["sn"]

into the following line :

$server->serial_number = $request["sn"]

One thought on “Laravel Error Message : Undefined index : column

Leave a Reply