Laravel 编辑浅层嵌套资源

问题描述

我使用以下资源使用 shallow nesting

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

得到以下包含两条记录的表格,这是 index.blade.php 的结果

enter image description here

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

enter image description here

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

enter image description here

如您所见,记录未更新

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

enter image description here

以及网络标签中的表单数据

enter image description here

并将数据发布到

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."));
}

这是模型(请注意,我使用的表的名称与默认情况下的预期名称不同 - 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);
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)