2016-06-28 126 views
0

我創建了一個視圖頁面,通過從數據庫獲取來在表格中顯示數據。我給了行動下降。下拉菜單中有兩個選項。查看/編輯和刪除。當我選擇編輯按鈕時,相應的行應從數據庫中刪除。我如何在laravel中編寫代碼?在正常的PHP我知道。任何人都可以幫助編寫代碼?我的看法頁面如下如何從laravel中的表中刪除特定的行5.2

@extends('app') 

@section('content') 


    <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> 
     </ol> 
     <div class="templatemo-content"> 

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

      <div> 
       <div> 
        <div> 

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

          {{--{{ UserController::getIndex() }}--}} 
          @foreach($name as $nam) 
           <tr> 
            <td>{{ $nam->userID }}</td> 
            <td>{{ $nam->description }}</td> 
            <td>{{ $nam->contactName }}</td> 
            <td>{{ $nam->contactPhone }}</td> 
            <td>{{ $nam->timeZone }}</td> 
            <td> 
             @if($nam->isActive == '1') 
              Yes 
              @else 
             No 
              @endif 
            </td> 
            <td>{{ date('Y/m/d H:i:s',($nam->lastLoginTime)) }}</td> 
            <td> 
             {{[email protected] (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> 
               </button> 
               <ul class="dropdown-menu" role="menu"> 
                {{[email protected] ($nam->isActive == 'Yes')--}} 
                 <li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $nam->userID }}"><a href="#">View/ Edit</a> 
                 </li> 
                {{[email protected]}} 
                <li><a href="{{ url('/user/delete/'.$nam->userID)}}">Delete</a></li> 
               </ul> 
              </div> 
             {{[email protected]}} 
            </td> 
           </tr> 
          @endforeach 
          </tbody> 
         </table> 
         {{$name->links()}} 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 

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

       </br> 




    <h4>Create a new User</h4> 
    {{--<form class="templatemo-preferences-form" role="form" method="POST" action="{{ action('[email protected]') }}">--}} 
     {{--<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> 
      <div class="row templatemo-form-buttons"> 
       <div class="submit-button"> 
        <button type="submit" class="btn btn-primary">New</button> 

       </div> 
      </div> 
      </div> 
    </form> 
{{--</form>--}} 


    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#example').dataTable(); 
     }); 
    </script> 
@endsection 

控制器頁給出的是

<?php 
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'; 
     $user->save(); 

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


     return redirect('userAdmin'); 
    } 


} 

請幫我做這件事...謝謝

+0

https://laravel.com/docs/5.2/queries#deletes –

回答

1

根本就以下命令來刪除記錄特定的記錄...

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

快樂編碼!

+0

謝謝老兄,它的工作...... !!! –

+0

歡迎@RahulVp。 –

相關問題