This tutorial will tell you today how to add data to Laravel in the form of Insert, Update or Delete. To insert data into Laravel, you should know about Laravel’s query, route and controller, basically how it works And what kind of insert is written in the laravel for inserting any data into the database. And how the query for the update is written. Step by step is given below. Now you can easily learn to insert, update or delete in laravel by following step by step.
resource/views/insert.blade.php
<!DOCTYPE html> <html> <head> <title>Insert,Update and Delete Create in Laravel</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> </head> <body> <form action="{{url('savedata')}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <div class="col-md-12"> <div class="form-group"> <label for="">Title</label> <input name="title" type="text" value="{{old('title')}}" class="form-control"> </div> <div class="form-group"> <label for="">Description</label> <textarea class="form-control" value="{{old('description')}}" rows="10" cols="105" name="description"></textarea> </div> </div> <div class="col-md-6"> <div class="box-footer"> <button type="submit" class="btn btn-primary">Save</button> </div> </div> </form> <h1>List Data</h1> <table id="example1" class="table table-bordered table-striped"> <thead> <tr> <th>S.No.</th> <th>Title</th> <th>Description</th> <th data-orderable="false">Action</th> </tr> </thead> <tbody> <?php $i=1; foreach ($index_url as $key => $value) { $title = $index_url[$key]->title; $page = $index_url[$key]->description; ?> <tr> <td>{{$i}}</td> <td>{{$title}}</td> <td><?PHP echo stripcslashes(substr($index_url[$key]->discription, 0, 149)); ?>..</td> <td> <a href="{{url('edit/'.$index_url[$key]->id)}}">Edit</a> | <a href="{{url('delete/'.$index_url[$key]->id)}}" onclick="return confirm('Are you sure? Delete ')">Delete</a> </td> </tr> <?php $i++; }?> </tbody> </table> </body> </html>
Now copy and paste this code by making a file of your resource/views/insert.blade.php. After that, you hit the url in the file of your route.php. How to hit the url.You can see the file in the route below.
routes/web.php
Route::post('savedata', 'DataController@savedata'); Route::get('edit/{id}', function ($id) { $edit_url = DB::table('blog')->select('*')->where('id', '=', $id)->get(); return view('edit')->with('edit_url', $edit_url); }); Route::post('Updateblog', 'DataController@Updateblog'); Route::get('delete/{id}', 'DataController@delete');
There is a file of web.php inside the route folder.In the same file, laravel url is hit.After this url, you will write down the code given below in the file of edit.blade.php.
resource/views/edit.blade.php
<!DOCTYPE html> <html> <head> <title>Update</title> </head> <body> <form action="{{url('Updateblog')}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <input type="hidden" name="id" value="{{ $edit_url[0]->id }}"> <div class="col-md-6"> <div class="form-group"> <label for="">Title</label> <input name="title" type="text" value="{{ $edit_url[0]->title }}" class="form-control"> <p class="help-block pull-right">*Required</p> </div> <div class="form-group"> <label for="">Page</label> <textarea name="description" class="form-control">{{$edit_url[0]->description}}</textarea> <p class="help-block pull-right">*Required</p> </div> </div> <div class="col-md-6"> <div class="box-footer"> <button type="submit" class="btn btn-primary">Update</button>| <a href="{{url('admin/add_index')}}" class="btn btn-primary">Back</a> </div> </div> </form> </body> </html>
Now we will work in our controller file, in which we will write all code related to query in this file. Like create update read delete query
DataController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use DB; use Redirect; use View; class DataController extends Controller { public function savedata(Request $request) { $title = $request->title; $description = $request->description; DB::table('blog')->insert([ 'title' =>$title, 'description' =>$description ]); return redirect()->back()->with('message', 'Thank you for Post Blog.'); } public function Updateblog(Request $request) { $id = $request->id; $title = $request->title; $description = $request->description; DB::table('blog')->where('id', $id)->update([ 'title' =>$title, 'description' =>$description ]); return redirect()->back()->with('message', 'Thank you for Post Blog Update.'); } public function delete($id){ $userans = DB::table('blog')->where('id', $id)->delete(); if($userans){ return redirect()->back()->with('message', 'One recoard deleted'); } } }