Status active inactive: user status active and inactive using jQuery in Laravel 5. Many web sites need to set up active and inactive option for active and deactivate user post or user information.
In this tutorial I will help you with an example and give all source code, which you can apply in your project or web application. So follow the example and source code:
I am crate a select option and set two option one is active and second is inactive. In the select field I am generate a onchange function and set the value in the on change function name. Thats’ like:
<select class="selectpicker" onchange="update_status(this.value,{{$val->u_id}})"> <option>Select Action</option> <option value="1">Active</option> <option value="0">Inactive</option> </select>
Using JavaScript pass the select value and ID value in a URL link using GET method. For example:
<script type="text/javascript"> function update_status(status,u_id){ var status = status; var u_id=u_id; $.ajax({ url: "update_user_status/{uid}", type: "GET", data: {status:status,u_id:u_id}, success: function(value){ if(value==1){ alert("success"); }else{ alert("error"); } } }); } </script>
If status update successful then show a success message or display an error message.
In the routes.php page generate a URL link and crate a controller function name. That is:
Route::get('update_user_status/{uid}','StatusController@update_user_status');
Go to the controller page and store the value in a variable and pass the value in the model page to define a model function. For example:
Controller:
<?php public static function update_user_status(){ $data=array('status'=>$_GET['status']); $uid=$_GET['u_id']; with(new StatusModel)->updateUserStatus($data,$uid); } ?>
In the model page update the user status using laravel update query and return some value or return true, if status update successful.
Model:
<?php public function ChangeUsereStatus($data,$uid){ $qry=DB::table('Table Name')->where('u_id',$uid)->update($data); if($qry){ return 1; }else{ return 0; } } ?>