onclick update database in laravel

Today, We want to share with you onclick update database in laravel.In this post we will show you Laravel Update User Status Using Toggle Button Example, hear for update database when user click on a link we will give you demo and example for implement.In this post, we will learn about Laravel 5/6/7 Change database column when button is clicked with an example.

onclick update database in laravel

There are the Following The simple About Ajax CRUD operations in laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop Insert Update and Delete record with AJAX in Laravel, so the Update database on button click? – PHP is used for this example is following below.

Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.

Keywords : onclick ajax in laravel, how to insert data using ajax in laravel with datatables, onclick update database in php, laravel ajax update database, update data in laravel, insert data using ajax in laravel 5, update query on button click in php, how to update table in laravel

Insert Update and Delete record with AJAX in Laravel

1. Table structure

CREATE TABLE `members` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `member_name` varchar(80) NOT NULL,
  `name` varchar(80) NOT NULL,
  `email` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Database Configuration

Open .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pakainfo
DB_USERNAME=root
DB_PASSWORD=

3. Model

Completed Code

orderBy('id', 'asc')->get(); 
     return $value;

   }

   public static function insertData($data){

     $value=DB::table('members')->where('member_name', $data['member_name'])->get();
     if($value->count() == 0){
       $insertid = DB::table('members')->insertGetId($data);
       return $insertid;
     }else{
       return 0;
     }

   }

   public static function updateData($id,$data){
      DB::table('members')->where('id', $id)->update($data);
   }

   public static function deleteData($id=0){
      DB::table('members')->where('id', '=', $id)->delete();
   }

}

4. Controller

Completed Code

input('name');
    $member_name = $request->input('member_name');
    $email = $request->input('email');

    if($name !='' && $member_name !='' && $email != ''){
      $data = array('name'=>$name,"member_name"=>$member_name,"email"=>$email);

      // Call insertData() method of Task Model
      $value = Task::insertData($data);
      if($value){
        echo $value;
      }else{
        echo 0;
      }

    }else{
       echo 'Fill all fields.';
    }

    exit; 
  }

  // Update record
  public function updateMember(Request $request){

    $name = $request->input('name');
    $email = $request->input('email');
    $editid = $request->input('editid');

    if($name !='' && $email != ''){
      $data = array('name'=>$name,"email"=>$email);

      // Call updateData() method of Task Model
      Task::updateData($editid, $data);
      echo 'Update successfully.';
    }else{
      echo 'Fill all fields.';
    }

    exit; 
  }

  // Delete record
  public function deleteMember($id=0){
    // Call deleteData() method of Task Model
    Task::deleteData($id);

    echo "Delete successfully";
    exit;
  }
}

5. Route

Open router/web.php file.


6. View

Create a new index.blade.php file in resources/views/ directory.



  
    Insert Update and Delete record with AJAX in Laravel
    
    
    
     
    
  
  

    
Membername Name Email

Update database onclick using ajax call

use this jQuery

$(document).ready(function(){
    $('li').on('click', function(e){
        $.ajax({
            url: 'api/products/update/'+$(this).data('id'),
            method: 'GET'
        }).then(function(){
            alert('success');
        });
    }); 
})

Blade File Code

@foreach($products as $product)
    
  • {!! $product->name !!}
  • @endforeach
    Web Programming Tutorials Example with Demo

    Read :

    Summary

    You can also read about AngularJS, ASP.NET, VueJs, PHP.

    I hope you get an idea about Update Model Status using Toggles in Laravel.
    I would like to have feedback on my infinityknow.com blog.
    Your valuable feedback, question, or comments about this article are always welcome.
    If you enjoyed and liked this post, don’t forget to share.

    Leave a Comment