mysql - 如何从 laravel 5.2 中的表中删除特定行

标签 mysql laravel-5

我创建了一个 View 页面,通过从数据库获取数据来显示表中的数据。我给出了 Action 下拉菜单。下拉菜单中有两个选项。查看/编辑和删除。当我选择编辑按钮时,相应的行应该从数据库中删除。我如何在 Laravel 中编写该代码?在正常的 php 中我知道。谁能帮忙写一下代码吗??我的查看页面如下

  @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>
                                        {{--@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>
                                                </button>
                                                <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>
                                                    {{--@endif--}}
                                                    <li><a href="{{ url('/user/delete/'.$nam->userID)}}">Delete</a></li>
                                                </ul>
                                            </div>
                                        {{--@endif--}}
                                    </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('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>
            <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

Controller 页面是

    <?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');
    }


}

请帮我做到这一点...谢谢

最佳答案

只需执行以下操作即可删除记录特定记录...

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

编码愉快!!!

关于mysql - 如何从 laravel 5.2 中的表中删除特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38073088/

相关文章:

php - 在 Laravel 5/6/7/8/9 中使用表单请求验证时出现错误 "This action is unauthorized."

php - 执行原始插入时如何在 Laravel 5.1 中强制更新时间戳...在重复键更新时?

PHP 语句应该返回多个对象,但只返回一个

mysql 在需要唯一行的地方加入

mysql - 检查 MySQL 中的数字与非数字列类型

mysql - 如何在 MySQL 中按地区对美国各州进行分组?

php - Apache 不会在轮询时提供服务

laravel-5 - Laravel 5 多级分类

php - Laravel5 Eloquent 从带有 where 子句的多个表中选择

laravel - 关于 Laravel 5.1 安全性