How to Show Edit Success Message in Laravel

Posted on

This article is about how to show edit success message in Laravel-web based application framework. The message itself is called an edit success message because it is actually a message presented upon the successful process of editing a value of a field or a column. For an example, if there is a process for editing submitted from an edit form to a controller as shown in the route definition below in a file named web.php located in the folder named ‘routes’ normally there in a Laravel version 5.3 above :

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

It is where the edit success message is going to be printed. First of all, let’s take a look at the edit form :

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

It will be submitted and processed further to a method named ‘update’ located in a controller file named ‘TechnologyCategoryController’. The method named ‘update’ is shown in the following :

public function update($id_technology) {

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

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

$input = Input::all();

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

As shown in the above snippet code, it is redirected to an URL of ‘/technology-category’. It is actually forwarded to the main URL of the module which is shown in the route file named web.php in the following line :

Route::get('/technology-category','TechnologyCategoryController@index');

And in the index method of TechnologyCategoryController file, it is actually displaying an index.blade.php file as shown in the following snippet code :

  public function index(Request $request) {
        
        $list_technology = TechnologyCategory::orderBy('id', 'DESC')->paginate(5);
        return view('technology-category.index', compact('list_technology'))
                        ->with('i', ($request->input('page', 1) - 1) * 5);

    }

But the important part of the line of code which is actually defining

@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif

So, the success is a variable passed from the previous method processing the update or the edit process. It is shown in the following line of code :

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

So, the line ‘<p>{{ $message }}</p>’ will be automatically replaced with ‘Technology Category updated successfully’.

One thought on “How to Show Edit Success Message in Laravel

Leave a Reply