提问者:小点点

Laravel销毁嵌套资源


我有一个资源名OrganizationEmailDomain

Route::resource('organizations.emaildomains', 'OrganizationEmailDomainController', ['except' => ['show']]);

并且能够很好地索引它

在这个视图中,当点击三个点时,就是破坏部分

<form action="{{ route('organizations.emaildomains.destroy', ['organization' => $id, 'emaildomain' => $email_domain->id]) }}" method="post">
    @csrf
    @method('delete')
    <button type="button" class="dropdown-item" onclick="confirm('{{ __("Are you sure you want to delete this email domain?") }}') ? this.parentElement.submit() : ''">
        {{ __('Delete') }}
    </button>
</form>

如果在该索引视图中,我回显$email_域(

[{"id":1,"email_domain":"test.com","organization_id":1,"created_at":"2021-03-02T14:46:50.000000Z","updated_at":"2021-03-02T14:46:56.000000Z"},{"id":2,"email_domain":"gmail.com","organization_id":1,"created_at":null,"updated_at":null}]

当我试图摧毁

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\OrganizationEmailDomain  $emailDomain
 * @return \Illuminate\Http\Response
 */
public function destroy(Organization $organization, OrganizationEmailDomain $emailDomain)
{
    //dd($emailDomain);

    $emailDomain->delete();

    return redirect()->route('organizations.emaildomains.index',$organization->id)->withStatus(__("Organization's email domain successfully deleted."));
}

dd($emailDomain)返回此

正如Langbox所说

(...)有许多关系(...)所以它总是返回集合

所以,从丹的回答中得到启发,只是替换了

$emailDomain->delete();

与以下

$org_email = OrganizationEmailDomain::where('id', $organization->org_email_domains->pluck('id'))->delete();

鉴于在组织模型中

/**
 * Get the email domains of the organization
 *
 * @return void
 */
public function org_email_domains()
{
    return $this->hasMany(OrganizationEmailDomain::class);
}

这里有什么问题?

它总是删除上一行中的记录。

例如,让我们删除dddddddddddddd

这删除了第一行,aaaaaaaaaaaaaaaaaaa

如果我尝试删除bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb。

以下是索引,仅供参考。刀身我之前共享的表单所在的php

<table class="table table-flush"  id="datatable-basic">
    <thead class="thead-light">
        <tr>
            <th scope="col">{{ __('Organization') }}</th>
            <th scope="col">{{ __('Email Domain') }}</th>
            <th scope="col">{{ __('Creation date') }}</th>
            @can('manage-org-emaildomains', App\User::class)
                <th scope="col"></th>
            @endcan
        </tr>
    </thead>
    <tbody>
        @foreach ($email_domains as $email_domain)
            <tr>
                <td>{{ $organization->name }}</td>
                <td>{{ $email_domain->email_domain }}</td>
                <td>{{ $email_domain->created_at ? $email_domain->created_at->format('d/m/Y H:i') : "NULL" }}</td>
                @can('manage-org-emaildomains', App\User::class)
                    <td class="text-right">
                        @if (auth()->user()->can('update', $email_domain) || auth()->user()->can('show', $email_domain) || auth()->user()->can('delete', $email_domain))
                            <div class="dropdown">
                                <a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <i class="fas fa-ellipsis-v"></i>
                                </a>
                                <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
                                    @can('show', $email_domain)
                                        <a class="dropdown-item" href="{{ route('organizations.emaildomains.show', ['organization' => $id, 'emaildomain' => $email_domain->id]) }}">{{ __('Show') }}</a>
                                    @endcan
                                    @can('update', $email_domain)
                                        <a class="dropdown-item" href="{{ route('organizations.emaildomains.edit', ['organization' => $id, 'emaildomain' => $email_domain->id]) }}">{{ __('Edit') }}</a>
                                    @endcan
                                    @if (auth()->user()->can('delete', $email_domain))
                                        <form action="{{ route('organizations.emaildomains.destroy', ['organization' => $id, 'emaildomain' => $email_domain->id]) }}" method="post">
                                            @csrf
                                            @method('delete')
                                            <button type="button" class="dropdown-item" onclick="confirm('{{ __("Are you sure you want to delete this email domain?") }}') ? this.parentElement.submit() : ''">
                                                {{ __('Delete') }}
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </div>
                        @endif
                    </td>
                @endcan
            </tr>
        @endforeach
    </tbody>
</table>

共2个答案

匿名用户

你不能只删除id,你只传递id。

在控制器中,您必须找到,例如:

$flight = Flight::find($emaidomain);

然后你做

$flight->delete();

您只是在表单中传递id,您必须通过id在控制器中找到该项目,然后删除,不能仅删除id。

匿名用户

我把路线改成了浅层筑巢

Route::resource('organizations.emaildomains', 'OrganizationEmailDomainController', ['except' => ['show']])->shallow();

然后表格到

<form action="{{ route('emaildomains.destroy', ['organization' => $id, 'emaildomain' => $email_domain->id]) }}" method="post">
    @csrf
    @method('delete')
    <button type="button" class="dropdown-item" onclick="confirm('{{ __("Are you sure you want to delete this email domain?") }}') ? this.parentElement.submit() : ''">
        {{ __('Delete') }}
    </button>
</form>

和破坏()到

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\OrganizationEmailDomain  $emailDomain
 * @return \Illuminate\Http\Response
 */
public function destroy(Request $request, OrganizationEmailDomain $emailDomain)
{
    $path = $request->path();

    $id = substr($path, strrpos($path, '/') + 1);

    $emailDomain = OrganizationEmailDomain::find($id);

    $emailDomain->delete();

    return redirect()->route('organizations.emaildomains.index',$request->query()['organization'])->withStatus(__("Organization's email domain successfully deleted."));
}

现在它工作得很好。