Update Multiple Field in a Database from Form Laravel

Posted on

In this article, based on the article titled ‘Update a Field in Database from Form Laravel‘ exists in this link , the functionality or the main focus is expanded instead of updating one field from the edit or update form, the update process will be implemented into several fields.

So, using the same snippet code utilized in the article titled ‘Update a Field in Database from Form Laravel‘ in this link , to simulate a multiple field condition to be updated, the previous snippet code will be added with several input type field as follows :

{{ Form::open(array('url' => '/server/check_resource', 'class' => 'form-horizontal', 'id' => 'resource_form')) }}
<input id="id_server" name="id_server" type="hidden" value="{{$data['server']->id_server}}"> 
<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>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Processor</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="Processor" name="processor"
value="{{$data['server']->processor}}">
</div>
<div class="form-group">
<label class="col-md-2 control-label">Harddisk</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="Harddisk" name="harddisk"
value="{{$data['server']->storage}}"> <b>In GB</b>
</div>
</div>
...
{{Form::close}}

The above is snippet code which is presented is actually representing a blade view template file for editing the resource available in a server. Resource in a server where it will normally available such as memory, processor and harddisk is updated and those values represented all the multiple fields. Those multiple fields will be updated. As it can be seen in the above snippet code, those fields are represented by the name attribute of ‘memory’, ‘processor’ and ‘harddisk’ respectively.

The update process is decided by the ‘target’ or ‘destination’ of the form. In the above snippet code, the target is defined in the ‘url’ attribute and the value is ‘/server/check_resource’. So, as usually configured in web application based on Laravel framework, the definition of the mapped URL so that it can be passed to be processed in another step can be seen in the route file configuration located in /routes/web.php in Laravel 5.3 as shown below :

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

Based on the above snippet code, the method name ‘checkResource’ in a controller file named ‘ServerController’ will be responsible for handling POST from any page requesting the URL of ‘/server/check_resource’. Below is the content of checkResource method where the update process of multiple fields will be carried out :

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

As it can be shown in the above snippet code, there are three fields included for retrieving values which is going to be used for updating the already stored values in the associated column respectively. Just store it into several variables where the values pulled from the input type field HTML element and in this case it is memory, processor and harddisk.

To list or to enumerate the field name with their associated value, just write it as shown in the above snippet code.

2 thoughts on “Update Multiple Field in a Database from Form Laravel

Leave a Reply