How to Execute Query Update Using DB Class where the data retrieved from Blade View Template Form using Input Class in Laravel

Posted on

This is another article depicting on how to do the thing mentioned in the title of this article. It is about how to execute query update using DB class where the data which is being used for the update process is retrieved from Blade View Template Form. The Input Class is used to retrieved every data from the every field in the controller file. This article is actually related with the article titled ‘How to Execute Query Update Using DB Class where the data retrieved from Blade View Template Form using Input Class in Laravel’ in this link.

The scenario is still the same, it is about updating the Technology Category edit form where it is related with the following files :

  1. A Blade View Template file form as shown below :
<div id="container">
<div id="row">
{{ Form::open(['url' => ['/technology-category/update', $data->id], 'class' => 'form-horizontal', 'method' => 'POST']) }}
<div class="form-group full-width-chart">
<label class="col-md-2 control-label">Category</label>
<div class="col-md-4">

<input type="text" class="form-control" placeholder="Category" name="cat_id" value="{{ $data->cat_id }}"/>
<span class="text-danger" id="name" />
</div>
</div>
<div class="form-group full-width-chart">
<label class="col-md-2 control-label">Nama</label>
<div class="col-md-4">

<input type="text" class="form-control" placeholder="Technology" name="name" value="{{ $data->name }}"/>
<span class="text-danger" id="name" />
</div>
</div>
<div class="form-group full-width-chart">
<div class="col-md-4 col-md-offset-2">
<input type="submit" class="btn btn-default" value="Submit">
<input type="submit" class="btn btn-default" value="Cancel">
</div
</div>
{{ Form::close() }}
</div>
</div>

2. Another file is the controller file named TechnologyCategoryController defined the above edit form for further editing. It is definitely going to be filled with the value retrieved from the database. It is shown in the method named ‘edit’ as shown below :

public function edit($id_technology) {

$selected_technology = DB::table('technology_category')
->where('id', '=', $id_technology)
->first();

return view('technology-category.edit')->with('data', $selected_technology);
}

So, the variable named ‘data’ will be filled with a variable named ‘$selected_technology’. The variable named data itself will be passed to the edit.blade.php located in the folder named ‘/resources/views/technology-category’ in the root folder of Laravel-web based installation.

3. After the editing process, it will be submitted to the URL of ‘/technology-category/update’ and it will be routed as shown in a file named ‘routes/web.php’ :

Route::post('/technology-category/update/{id_technology}','TechnologyCategoryController@update');

4. The method called update will handle the process for updating database with the changed value from the edit form as shown in the following lines :

public function update($id_technology) {

$technology = TechnologyCategory::find($id_technology);
$cat_id = Input::get('cat_id');
$name = Input::get('name');

DB::table('technology_category')->where('id',$id_technology)->update(array('cat_id'=>$cat_id,'name'=>$name));

return redirect('/technology-category')
->with('success', 'Technology Category updated successfully');
}

The field is retrieved by stating the value of the attribute name from each field changed or edited as shown in the following example :

$cat_id = Input::get('cat_id');

It is also passed to the method called from a DB class as shown below :

DB::table('technology_category')->where('id',$id_technology)->update(array('cat_id'=>$cat_id,'name'=>$name));

Since there are several fields, it in encapsulated in the form of an array as shown above.

One thought on “How to Execute Query Update Using DB Class where the data retrieved from Blade View Template Form using Input Class in Laravel

Leave a Reply