Rahul Vp Rahul Vp - 3 months ago 41x
MySQL Question

how to do delete of particular row from a table in laravel 5.2

I created one view page, there displaying data in a table by fetching from database. and i given Action drop down . Two options are there in the drop down. View/edit and Delete. When i select the edit button, the corresponding row should delete from the database. How can i write that code in laravel?. In normal php i know. Can any one help to write the code??. My view page is given below



<div class="templatemo-content-wrapper" xmlns="http://www.w3.org/1999/html">
<ol class="breadcrumb">
<li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
<li class="active">user information</li>
<div class="templatemo-content">

<h1>View/Edit user information</h1>


<table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc">
<h3>Select User :</h3>
<th>User ID</th>
<th>User Desc</th>
<th>Contact Name</th>
<th>Contact Email</th>
<th>Time Zone</th>
<th>Last Login (GMT+05:30)</th>

{{--{{ UserController::getIndex() }}--}}
@foreach($name as $nam)
<td>{{ $nam->userID }}</td>
<td>{{ $nam->description }}</td>
<td>{{ $nam->contactName }}</td>
<td>{{ $nam->contactPhone }}</td>
<td>{{ $nam->timeZone }}</td>
@if($nam->isActive == '1')
<td>{{ date('Y/m/d H:i:s',($nam->lastLoginTime)) }}</td>
{{--@if ( in_array($nam->isActive, array('Yes','No')) )--}}

<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
<ul class="dropdown-menu" role="menu">
{{--@if ($nam->isActive == 'Yes')--}}
<li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $nam->userID }}"><a href="#">View/ Edit</a>
<li><a href="{{ url('/user/delete/'.$nam->userID)}}">Delete</a></li>

<input type="submit" id="submit" name="submit" value="View" class="button">
<a href="{{ url('user/add') }}"> <input type="submit" id="add" name="add" value="Edit" class="button"></a>


<h4>Create a new User</h4>
{{--<form class="templatemo-preferences-form" role="form" method="POST" action="{{ action('UserController@save') }}">--}}
{{--<input type="hidden" name="_token" value="{{ csrf_token() }}">--}}

<form role="form" method="POST" action="{{ url('userAdmin') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="row">
<div class="col-md-6 margin-bottom-15">

<input type="text" class="form-control" name="userID" value="{{ old('userID') }}" placeholder="Enter User ID">
<div class="row templatemo-form-buttons">
<div class="submit-button">
<button type="submit" class="btn btn-primary">New</button>


<script type="text/javascript">
$(document).ready(function() {
} );

controller page is

namespace App\Http\Controllers;
use Mail;
use Illuminate\Support\Facades\DB;
use Faker\Provider\DateTime;
use App\User;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Request;

class UserController extends Controller
public $type = 'User';

public function getIndex()

$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);

public function getData()

$name = DB::table('user');
return view('user.add')->with('name', $name);

public function userInsert()
$postUser = Input::all();
//insert data into mysql table
$data = array('userID'=> $postUser['userID']
// echo print_r($data);
$ck = 0;
$ck = DB::table('user')->Insert($data);
//echo "Record Added Successfully!";
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);


public function delete($id)
$user = Bookings::find($id);
$user->status = 'Not Confirmed';

$currUsr = User::find($user->userID);

return redirect('userAdmin');


Please help me to do this... Thanks


Simply do following to delete record specific records...

public function delete($id)
    DB::table('user')->where('userID', '=', $id)->delete();
    return redirect('userAdmin');

Happy Coding!!!