Posted inTechnology / Ajax / JavaScript / jQuery / Laravel / Mysql / Mysqli / php / Programming

Laravel 6 Insert Update and Delete record from MySQL

Today, We want to share with you Laravel 6 Insert Update and Delete record from MySQL.In this post we will show you Tutorial – Insert Update And Delete Record With AJAX In Laravel 6.2 Example, hear for delete data from database in laravel 6 we will give you demo and example for implement.In this post, we will learn about update record in laravel 6.2 using eloquent with an example.

Laravel 6 Insert Update and Delete record from MySQL

There are the Following The simple About simple crud application in laravel 6 Full Information With Example and source code.

As I will cover this Post with live Working example to develop CRUD (Create Read Update Delete) in a Laravel 6 App, so the Laravel 6 Simple CRUD (Add, Edit, Delete, View) is used for this example is following below.

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

Step 2. Database Configuration

create a .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=member_group_info
DB_USERNAME=root
DB_PASSWORD=FDG45456%^455sd

Step 3. Create a Laravel 6 Model

app/Profile.php

php artisan make:model Profile
orderBy('id', 'asc')->get(); 
    }else{
      $member_value=DB::table('members')->where('id', $id)->first();
    }
    return $member_value;
  }

  public static function insertData($data){
    $member_value=DB::table('members')->where('member_name', $data['member_name'])->get();
    if($member_value->count() == 0){
      DB::table('members')->insert($data);
      return 1;
     }else{
       return 0;
     }
 
  }

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

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

step 4. Create a Laravel Controller

app/Http/Controllers/ProfileController.php

0){
      $memberData['editMemberData'] = Profile::getmemberData($id);
    }

    // Pass to view Member
    return view('index')->with("memberData",$memberData);
  }

  public function save(Request $request){
 
    if ($request->input('submit') != null ){

      // Update Member record
      if($request->input('editid') !=null ){
        $name = $request->input('name');
        $email = $request->input('email');
        $editid = $request->input('editid');

        if($name !='' && $email != ''){
           $data = array('name'=>$name,"email"=>$email);
 
           // Update Member Data
           Profile::updateData($editid, $data);

           Session::flash('message','Update Member Profile successfully.');
 
        }
 
      }else{ // Insert Member record
         $name = $request->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);
 
            // Member Insert
            $value = Profile::insertData($data);
            if($value){
              Session::flash('message','Insert successfully.');
            }else{
              Session::flash('message','Member Name already exists.');
            }
 
         }
      }
 
    }
    return redirect()->action('ProfileController@index',['id'=>0]);
  }

  public function deleteMember($id=0){

    if($id != 0){
      // Member Delete
      Profile::deleteData($id);

      Session::flash('message','Delete successfully.');
      
    }
    return redirect()->action('ProfileController@index',['id'=>0]);
  }
}

Step 5 : Define a Laravel Route

routes/web.php


Setp 6. View

resources/views/index.blade.php

Completed full source Code



 
   
@if(Session::has('message'))

{{ Session::get('message') }}

@endif @foreach($memberData['data'] as $member) @endforeach
Membername Name Email
{{ csrf_field() }}
{{ $member->member_name }} {{ $member->name }} {{ $member->email }} Update Delete
@if($memberData['edit'])

Edit record

{{ csrf_field() }}
Membername
Name
Email
 
@endif
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 Laravel 6 Insert Update Delete in Mysql Table.
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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype