简体   繁体   English

将数据从一个表复制到另一个表,并使用 laravel 中的一个按钮从主表中删除数据

[英]copy data from one table to another table and also delete data from main table with one button in laravel

i have an admin table in my laravel project.i want to add admin's data in another table and delete it when i click on remove button我的 laravel 项目中有一个管理员表。我想在另一个表中添加管理员数据并在单击删除按钮时将其删除

this my admin controller这是我的管理员 controller

 public function remove(Request $request,Admin $admin)
{
    Deletedadmin::create([
        'first_name'=>$request['first_name'],
        'last_name'=>$request['last_name'],
        'username'=>$request['username'],
        'email'=>$request['email'],
        'mobile'=>$request['mobile'],
        'age'=>$request['age'],
        'password'=>0,
    ]);
    return redirect()->route('admin.index');

}

this my admin index code这是我的管理索引代码

<div class="text-center mb-1">
                                    <form action="{{route('admin.remove',['admin'=>$admin->id])}}" method="POST">
                                        @csrf
                                        <input class="form-control" type="hidden" placeholder="First Name"
                                               name="first_name" value="{{$admin->first_name}}">
                                        <input class="form-control" type="hidden" placeholder="Last Name"
                                               name="last_name"value="{{$admin->last_name}}">
                                        <input class="form-control" type="hidden" placeholder="Username"
                                               name="username"value="{{$admin->username}}">
                                        <input class="form-control" type="hidden" placeholder="Email"
                                               name="email"value="{{$admin->email}}">
                                        <input class="form-control" type="hidden" placeholder="Mobile"
                                               name="mobile"value="{{$admin->mobile}}">
                                        <input class="form-control" type="hidden" placeholder="Age" name="age"value="{{$admin->age}}">

                                        <input class="btn btn-sm btn-warning" type="submit" value="Remove"name="remove_admin">
                                    </form>
                                </div>

i have seted my input type to hidden to not shown in my admin list and when i click on submit button it copy the inputs data from this table to removedadmin table我已将我的输入类型设置为隐藏,以不显示在我的管理列表中,当我单击提交按钮时,它将输入数据从该表复制到删除管理

now i want to know what can i do to first copy the data from admin to deletedadmins table then remove it from admin table现在我想知道我该怎么做才能首先将数据从 admin 复制到 deletedadmins 表然后将其从 admin 表中删除

i know how to do these actions seperately but i want to know can create and delete with on button??我知道如何单独执行这些操作,但我想知道可以使用 on 按钮创建和删除吗?

Sure you can delete the record from the admin table Just write the delete operation before the return line Like this:肯定可以删除admin表中的记录 只需要在return行前写delete操作就行了 像这样:

public function remove(Request $request, Admin $admin)
{
    Deletedadmin::create([
        'first_name' => $request['first_name'],
        'last_name'  => $request['last_name'],
        'username'   => $request['username'],
        'email'      => $request['email'],
        'mobile'     => $request['mobile'],
        'age'        => $request['age'],
        'password'   => 0,
    ]);
    $admin->delete(); // This will delete the main record
    return redirect()->route('admin.index');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM