Laravel雄辩删除

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel雄辩删除相关的知识,希望对你有一定的参考价值。

你好寻找助手,如何理解Laravel Eloquent模型。我需要删除User by Id,但我有表Main_info,Personal_info,Holidays,other_info,fm_day,oth_daysoff。在他们所有我插入UserId,我需要从所有表中删除选定的UserId?也许有人可以帮助我如何用Laravel Eloquent模型做到这一点?这是我的删除提交按钮和帖子方法的视图:

<div class="container">
<div class="row">

    <div class="col">
        <div class="card">
            <div class="card-head-man">
                <span>Filter type: </span>&nbsp;
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Position</a>&nbsp;
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Active/No</a>&nbsp;
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Employment type</a>
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Location</a>
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">List of employees</h3>
            </div>

            <div class="table-responsive">
                <table class="table card-table table-vcenter text-nowrap">
                    <thead>
                    <tr>
                        <th class="w-1"><a href="#" class="sort-act">ID<span class="caret"></span></a></th>
                        <th><a href="#" class="sort-act">First Name<span class="caret"></span></a></th>
                        <th>Last Name</th>
                        <th>Position</th>
                        <th>Active/no</th>
                        <th>Employment type</th>
                        <th>Location</th>
                        <th></th>
                        <th></th>
                    </tr>
                    </thead>

                    <tbody>
                    @foreach($allData as $data)


                        <tr>
                        <td><span class="text-muted">{{ $data->id }}</span></td>
                        <td>{{ $data->first_name }}</td>
                        <td>
                            {{ $data->last_name }}
                        </td>
                        <td>
                            {{ $data->position }}
                        </td>
                        <td>
                            {{ $data->employment_type }}
                        </td>
                        <td>
                            <span class="status-icon bg-success"></span> Active
                            <span class="status-icon bg-danger"></span> No
                        </td>
                        <td>{{ $data->location }}</td>

                        <td class="text-right">
                            <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Manage</a>
                            <div class="dropdown">
                                <form action="/deleteFromHome" method="post">
                                    {{csrf_field()}}
                                    <input style="display: none;" type="text" name="deleteUserId" class="form-control" id="inputName3" value="{{ $data->id }}">
                                    <button type="submit" class="btn btn-secondary btn-sm">Delete</button>
                                </form>
                            </div>
                        </td>
                        <td>
                            <a class="icon" href="javascript:void(0)">
                                <i class="fe fe-edit"></i>
                            </a>
                        </td>
                    </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>

</div>

这是我的控制器我现在是怎么做的:

public function deleteUserFromHome(Request $req)
{
    $userId = $req->input('deleteUserId');

    OtherInfo::where('id', $userId)->delete();
    OthDaysInfo::where('id', $userId)->delete();
    HolidaysInfo::where('id', $userId)->delete();
    FMdaysInfo::where('id', $userId)->delete();
    PersonalInfo::where('id', $userId)->delete();
    PersonalInfo::where('id', $userId)->delete();
    MainInfo::where('id', $userId)->delete();

    return redirect('home');
}

也许有一些更好的方法可以删除所有表中关于所选userId的所有数据?我需要一些例子来说明如何使用函数HasMany或其他函数删除Laravel Eloquent模型?

答案

您可以在创建表迁移时使用->onDelete('cascade')。例如,

// consider a sample migration below for a table with a foreign key on your users table
Schema::create('main_info', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->foreign('user_id')
          ->references('id')
          ->on('users')
          ->onDelete('cascade');
    $table->timestamps();
});

然后您可以轻松删除指定的用户,Laravel将负责删除它的子记录。

$user = User::find($id);
$user->delete(); // this will also delete any record for this user on main_info table

Read more here

以上是关于Laravel雄辩删除的主要内容,如果未能解决你的问题,请参考以下文章

如何编写相同的代码并以雄辩的关系获取相同的数据... laravel

Laravel 5 雄辩的查询混淆

Laravel 5.4雄辩的一对多关系

雄辩的laravel错误

Laravel - 雄辩的自我关系

Laravel 4.2 雄辩的动态查询