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
<?php namespace App; use Illuminate\Support\Facades\DB; use Illuminate\Database\Eloquent\Model; class Profile extends Model { public static function getmemberData($id=0){ if($id==0){ $member_value=DB::table('members')->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
<?php namespace App\Http\Controllers; use Session; use Illuminate\Http\Request; use App\Profile; class ProfileController extends Controller{ public function index($id=0){ // Fetch all Member records $memberData['data'] = Profile::getmemberData(); $memberData['edit'] = $id; // Fetch Member edit record if($id>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('[email protected]',['id'=>0]); } public function deleteMember($id=0){ if($id != 0){ // Member Delete Profile::deleteData($id); Session::flash('message','Delete successfully.'); } return redirect()->action('[email protected]',['id'=>0]); } }
Step 5 : Define a Laravel Route
routes/web.php
<?php //Member [email protected] Route::get('/', '[email protected]'); // localhost:8000/ Route::get('/{id}', '[email protected]'); Route::post('/save', '[email protected]'); Route::get('/deleteMember/{id}', '[email protected]');
Setp 6. View
resources/views/index.blade.php
Completed full source Code
<!doctype html> <html> <body> <form method='post' action='/save'> <!--Member Message --> @if(Session::has('message')) <p >{{ Session::get('message') }}</p> @endif <!-- Add/List records --> <table border='1' style='border-collapse: collapse;'> <tr> <th>Membername</th> <th>Name</th> <th>Email</th> <th></th> </tr> <tr> <td colspan="4">{{ csrf_field() }}</td> </tr> <!-- Add Member--> <tr> <td><input type='text' name='member_name'></td> <td><input type='text' name='name'></td> <td><input type='email' name='email'></td> <td><input type='submit' name='submit' value='Add'></td> </tr> <!-- List Member--> @foreach($memberData['data'] as $member) <tr> <td>{{ $member->member_name }}</td> <td>{{ $member->name }}</td> <td>{{ $member->email }}</td> <td><a href='/{{ $member->id }}'>Update</a> <a href='/deleteMember/{{ $member->id }}'>Delete</a></td> </tr> @endforeach </table> </form> <!-- Edit Member--> @if($memberData['edit']) <form method='post' action='/save'> <table> <tr> <td colspan='2'><h1>Edit record</h1></td> </tr> <tr> <td colspan="2">{{ csrf_field() }}</td> </tr> <tr> <td>Membername</td> <td><input type='text' name='m_name' readonly value='{{ $memberData["editMemberData"]->member_name }}' ></td> </tr> <tr> <td>Name</td> <td><input type='text' name='name' value='{{ $memberData["editMemberData"]->name }}'></td> </tr> <tr> <td>Email</td> <td><input type='email' name='email' value='{{ $memberData["editMemberData"]->email }}' ></td> </tr> <tr> <td> <input type='hidden' value='{{ $memberData["edit"] }}' name='editid'></td> <td><input type='submit' name='submit' value='Submit'></td> </tr> </table> </form> @endif </body> </html>
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.