Laravel 编辑浅嵌套资源

Laravel edit shallow nested resource

我正在使用以下资源 shallow nesting

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

得到以下 table 和两条记录,这是 index.blade.php

的结果

假设我们要将 tiagoperes.eu 编辑为 whosebug.com。我会转到编辑视图

更改相应的字段并单击保存按钮。这是结果

如您所见,记录未更新

在控制器的update()中检查$request->all()

以及网络选项卡中的表单数据

并将数据发布到

http://localhost/app/public/emaildomains/7

与浅嵌套中的 URI 匹配。


在edit.blade.php中我有以下表格来处理更新

<form method="post" action="{{ route('emaildomains.update', ['emaildomain' => $email_domain->id]) }}" autocomplete="off">
    @csrf
    @method('put')

    <h6 class="heading-small text-muted mb-4">{{ __('Email Domain information') }}</h6>
    <div class="pl-lg-4">
        <div class="form-group{{ $errors->has('organization_id') ? ' has-danger' : '' }}">
            <label class="form-control-label" for="input-organization_id">{{ __('Organization') }}</label>
            <select name="organization_id" id="input-organization" class="form-control{{ $errors->has('organization_id') ? ' is-invalid' : '' }}" placeholder="{{ __('Organization') }}" required>
                @foreach ($organizations as $organization)
                    <option value="{{ $organization->id }}" {{ $organization->id == old('organization_id', $email_domain->organization->id) ? 'selected' : '' }}>{{ $organization->name }}</option>
                @endforeach
            </select>
            @include('alerts.feedback', ['field' => 'organization_id'])
        </div>

        <div class="form-group{{ $errors->has('email_domain') ? ' has-danger' : '' }}">
            <label class="form-control-label" for="input-email_domain">{{ __('Email Domain') }}</label>
            <input type="text" name="email_domain" id="input-email_domain" class="form-control{{ $errors->has('email_domain') ? ' is-invalid' : '' }}" placeholder="{{ __('Email Domain') }}" value="{{ old('email_domain', $email_domain->email_domain) }}" required autofocus>

            @include('alerts.feedback', ['field' => 'email_domain'])
        </div>

        <div class="text-center">
            <button type="submit" class="btn btn-success mt-4">{{ __('Save') }}</button>
        </div>
    </div>
</form>

这里是 OrganizationEmailDomainController

中的编辑和更新方法
/**
 * Show the form for editing the specified resource.
 *
 * @param  \App\OrganizationEmailDomain  $email_domain
 * @return \Illuminate\Http\Response
 */
public function edit(Request $request, OrganizationEmailDomain $email_domain, Organization $model)
{
    $path = $request->path();

    $id = (int)explode('/', $path)[1];

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

    return view('organizations.emaildomains.edit', ['email_domain' => $emailDomain->load('organization'), 'organizations' => $model::where('id', $emailDomain->organization_id)->get(['id', 'name'])]);        

}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\OrganizationEmailDomain  $email_domain
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, OrganizationEmailDomain $email_domain)
{

    $email_domain->update($request->all());

    $organization_id = (int)$request->all()['organization_id'];

    return redirect()->route('organizations.emaildomains.index', ['organization' => $organization_id])->withStatus(__("Org's email domain successfully updated."));
}

这是模型(注意我使用的 table 与默认预期的名称不同 - protected $table = 'email_domains';

class OrganizationEmailDomain extends Model
{
    protected $fillable = [
        'email_domain', 'organization_id'
    ];

    protected $table = 'email_domains';

    /**
     * Get the organization
     *
     * @return \Organization
     */
    public function organization()
    {
        return $this->belongsTo(Organization::class);
    }

}

将模型 ID 注入路由或控制器操作时,您通常会查询数据库以检索与该 ID 对应的模型。 Laravel 路由模型绑定提供了一种方便的方法来自动将模型实例直接注入到您的路由中。例如,您可以注入与给定 ID 匹配的整个 OrganizationEmailDomain 模型实例,而不是注入 OrganizationEmailDomain 的 ID。

Laravel 自动解析在路由或控制器操作中定义的 Eloquent 模型,其类型提示变量名称与路由段名称匹配。例如:

use App\OrganizationEmailDomain;

Route::put('emaildomains/{emailDomain}', [OrganizationEmailDomainController::class, 'update']);

那么下面应该在你的控制器中

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\OrganizationEmailDomain  $emailDomain
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, OrganizationEmailDomain $emailDomain)
{

    $emailDomain->update($request->all());

    return redirect()->route('organizations.emaildomains.index', [
            'organization' => $request->organization_id
    ])->withStatus(__("Org's email domain successfully updated."));
}

注意,如果变量名$emailDomain与路线段{emailDomain}不同,则laravel将无法解析模型。因此,您将得到一个空的 OrganizationEmailDomain 模型,并且它不会更新任何数据。所以一定要在路由中定义相同的名称。

要检查路线的正确名称 运行 命令 php artisan route:list 您将看到路线和路段的名称。


编辑

为了解决问题,我运行

php artisan route:list

显示

organizations/{organization}/emaildomains/{emaildomain} | organizations.emaildomains.update

因此,将 OrganizationEmailDomainController.php 更改为

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\OrganizationEmailDomainRequest  $request
 * @param  \App\OrganizationEmailDomain  $emaildomain
 * @return \Illuminate\Http\Response
 */
public function update(OrganizationEmailDomainRequest $request, OrganizationEmailDomain $emaildomain)
{

    $emaildomain->update($request->all());

    return redirect()->route('organizations.emaildomains.index', ['organization' => $request->organization_id])->withStatus(__("Org's email domain successfully updated."));
}

够了。

请注意,唯一需要的更改是在控制器中从 $email_domain$emaildomain,但也删除了不必要的位以获得 organization_id 并使用上面建议的 $请求。