Update a Field in Database from Form Laravel

Posted on

This article consists of writing specifically made to share on how to update a field in database from form in a web application based on Laravel framework. The main purpose is to update the value of a single field in a database where the value itself is acquired from a form defined in a blade view template file.

As we already knew, the blade view template file is a file which represents the view or the page which is presented directly to user. In this case for an example, there is a form where inside the form itself there are lots of field or input type. Below is the snippet code which is part of the blade view template file :

<div class="tab-pane" id="resource">
<div class="row">
{{ Form::open(array('url' => '/server/check_resource', 'class' => 'form-horizontal', 'id' => 'resource_form')) }}

<div class="form-group">
<label class="col-md-2 control-label">Memory</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="Memory" name="memory"
value="{{$data['server']->memory}}"> <b>In GB</b>
<span class="text-danger" id="memory" />
</div>
...
{{ Form:close }}

The value which is going to be updated actually come from the input type field defined above. It is shown in the following snippet code :

<input type="text" class="form-control" placeholder="Memory" name="memory" value="{{$data['server']->memory}}"> <b>In GB</b>

The value of the text input type HTML element is filled by a certain method inside a controller file which is not discussed in this article. The important part is the name attribute of the element itself. It is going to be used as a reference in the controller later on to extract the element’s value. The name of the element or the input type HTML element itself  is ‘memory’. This is the element where the value of it is going to be updated.

As shown in the above snippet code, the one which is responsible for defining the form is the following snippet code :

{{ Form::open(array('url' => '/server/check_resource', 'class' => 'form-horizontal', 'id' => 'resource_form')) }}
...
{{ Form:close }}

The above definition is actually pointing on several important information such as the name of the form and the target the form. The form id is ‘resource_form’ which is considered as the reference to point out the form itself. Another important attribute which is owned by the form is  the destination target of the form in order to be processed further. The attribute is ‘url’ and it point out to ‘/server/check_resource’.

Then in the route file that URL stated before, ‘/server/check_resource’ defined as the main target must be mapped. In Laravel 5.3, the file is located in routes/route.php and the mapping definition is shown below :

Route::post('/server/check_resource','ServerController@checkResource');

So, in order to process the form using the defined script above, the method checkResource must be made in the controller file named ‘ServerController’. Below is the actual script for processing the value passed from the form so that it can be used for updating the column or field in the table :

public function checkSystemEdit(Request $request) {
        $id_server = $request->id_server;
        $memory = $request->memory;
        DB::table('server')
                ->where('id_server', $id_server)
                ->update(['memory' => $memory]);
}

It is possible to just summon or call the name of the input type field as shown above :

$memory = $request->memory;

Since the name of the field is ‘memory’, just stated as the above line of code to retrieve the updated value of input type field named ‘memory’. After that, to update the specific record or row of the table, get the associated id from the edit form represented by the following line of code below :

$id_server = $request->id_server;

After getting the right id of the associated record, the following line of code will specifically executed to update the record :

DB::table('server') ->where('id_server', $id_server) ->update(['memory' => $memory]);

 

 

3 thoughts on “Update a Field in Database from Form Laravel

Leave a Reply