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

Posted on

This is another written article focusing on Laravel web-based application where the main discussion is specified in the title of this article. It is about how to execute query update using DB class in Laravel web-based application. In order to update a field or several fields in a table or several tables using DB Class in Laravel, there is a function or a method which is part of the DB Class that can be called to perform it. The article for updating a single field is actually written in the article titled ‘Update a Field in Database from Form Laravel’ in this link and the other one on updating multiple field can be read in the article titled ‘Update Multiple Field in a Database from Form Laravel’ in this link.

But in this article, instead of using Request Class to acquire the data inserted from the blade view template file, it can also be done by using Input Class as the one which is going to be shown in the following example with the scenario  :

1. A blade view template file which is normally located in ‘/resources/views’ folder inside the root folder of Laravel web-based installation. Since it is actually a module for editing a category of technology, it is 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">Name</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>

The above snippet code is an entry form to edit a technology category which is considered as a technology catalog defined as part of TOGAF Technology Standard Architecture Catalog, such as Technology Database Service, Technology Application Service, Technology Mail Service, etc.

2. Fill the edit form above with the value which has already been initiated in the controller named TechnologyCategoryController in this context. In Laravel, a controller can be utilized to be able to reconstruct the value which is going to be presented in the blade view template. So, below is the edit method provided in it :

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

}

Every field which in the edit form above will be filled with the value retrieved from the database  shown in this line as shown in the method above :

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

It is actually only consists of two fields, Category Name of the Technology Category and also the name of the Technology itself, where it will be filled by passing a variable named ‘$selected_technology’ as shown in the following line extracted from the above snippet code :

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

3. Prepare the method for updating the content or the value of each field of the above edit form. It is also defined in the controller named TechnologyCategoryController as shown below :

public function update($id_technology) {

$technologyCategory = TechnologyCategory::find($id_technology);

 $input = Input::all();

$technologyCategory->update($input);

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

At the above snippet code, there are the parts which is functioning as the update process of the database using the changed value from the edit form. Using the Input class form, it is updating the Model named TechnologyCategory as shown in the following line of code :

$technologyCategory->update(Input::all());

Laravel detecting the field which is being extracted is by the ‘name’ attribute defined as shown below for an example :

<input type="text" class="form-control" placeholder="Technology" name="cat_id" value="{{ $data->cat_id }}"/>

It is also detecting the field column of the table which is going updated based on the name of the attribute. Based on the above example, the name of the field is ‘cat_id’. So, it will find the similar name with the column exist or available in the table associated by the TechnologyCategory class. The column named ‘cat_id’ and the table is referred as stated in the TechnologyCategory class as shown below :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TechnologyCategory extends Model
{
    //
    protected $table = 'technology_category';

    protected $primaryKey = 'id';

    protected $fillable = ['name'];

    public $timestamps = false;
        
}

The table named ‘technology_category’ as shown in the property named ‘$table’ of the class named TechnologyCategory.

2 thoughts on “How to Execute Query Update Using Model Class where the data retrieved from Blade View Template Form using Input Class in Laravel

Leave a Reply