title: Laravel edit shallow nested resource
body: <p>I'm using the following resource using <a href="https://laravel.com/docs/8.x/controllers#shallow-nesting" rel="nofollow noreferrer">shallow nesting</a></p>
  <pre><code>Route::resource('organizations.emaildomains', 'OrganizationEmailDomainController', ['except' =&gt; ['show']])-&gt;shallow();
  </code></pre>
  <p>Got the following table with two records which is the result of an index.blade.php</p>
  <p><a href="https://i.stack.imgur.com/QMDeV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QMDeV.png" alt="enter image description here" /></a></p>
  <p>Let's say we want to edit <code>tiagoperes.eu</code> to <code>stackoverflow.com</code>. I'd go to the edit view</p>
  <p><a href="https://i.stack.imgur.com/RDiRf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RDiRf.png" alt="enter image description here" /></a></p>
  <p>change the respective field and click the save button. This is the result</p>
  <p><a href="https://i.stack.imgur.com/MjFqU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MjFqU.png" alt="enter image description here" /></a></p>
  <p>As you can see, <strong>the record isn't updated</strong>.</p>
  <p>Checking the <code>$request-&gt;all()</code> in the update() of the controller</p>
  <p><a href="https://i.stack.imgur.com/0FyGx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0FyGx.png" alt="enter image description here" /></a></p>
  <p>and the form data in the network tab</p>
  <p><a href="https://i.stack.imgur.com/HgPPQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HgPPQ.png" alt="enter image description here" /></a></p>
  <p>and the data is posted to</p>
  <pre><code>http://localhost/app/public/emaildomains/7
  </code></pre>
  <p>which matches the URI in shallow nesting.</p>
  <hr />
  <p>In edit.blade.php I have the following form to handle the updates</p>
  <pre><code>&lt;form method=&quot;post&quot; action=&quot;{{ route('emaildomains.update', ['emaildomain' =&gt; $email_domain-&gt;id]) }}&quot; autocomplete=&quot;off&quot;&gt;
      @csrf
      @method('put')
  
      &lt;h6 class=&quot;heading-small text-muted mb-4&quot;&gt;{{ __('Email Domain information') }}&lt;/h6&gt;
      &lt;div class=&quot;pl-lg-4&quot;&gt;
          &lt;div class=&quot;form-group{{ $errors-&gt;has('organization_id') ? ' has-danger' : '' }}&quot;&gt;
              &lt;label class=&quot;form-control-label&quot; for=&quot;input-organization_id&quot;&gt;{{ __('Organization') }}&lt;/label&gt;
              &lt;select name=&quot;organization_id&quot; id=&quot;input-organization&quot; class=&quot;form-control{{ $errors-&gt;has('organization_id') ? ' is-invalid' : '' }}&quot; placeholder=&quot;{{ __('Organization') }}&quot; required&gt;
                  @foreach ($organizations as $organization)
                      &lt;option value=&quot;{{ $organization-&gt;id }}&quot; {{ $organization-&gt;id == old('organization_id', $email_domain-&gt;organization-&gt;id) ? 'selected' : '' }}&gt;{{ $organization-&gt;name }}&lt;/option&gt;
                  @endforeach
              &lt;/select&gt;
              @include('alerts.feedback', ['field' =&gt; 'organization_id'])
          &lt;/div&gt;
  
          &lt;div class=&quot;form-group{{ $errors-&gt;has('email_domain') ? ' has-danger' : '' }}&quot;&gt;
              &lt;label class=&quot;form-control-label&quot; for=&quot;input-email_domain&quot;&gt;{{ __('Email Domain') }}&lt;/label&gt;
              &lt;input type=&quot;text&quot; name=&quot;email_domain&quot; id=&quot;input-email_domain&quot; class=&quot;form-control{{ $errors-&gt;has('email_domain') ? ' is-invalid' : '' }}&quot; placeholder=&quot;{{ __('Email Domain') }}&quot; value=&quot;{{ old('email_domain', $email_domain-&gt;email_domain) }}&quot; required autofocus&gt;
  
              @include('alerts.feedback', ['field' =&gt; 'email_domain'])
          &lt;/div&gt;
  
          &lt;div class=&quot;text-center&quot;&gt;
              &lt;button type=&quot;submit&quot; class=&quot;btn btn-success mt-4&quot;&gt;{{ __('Save') }}&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>and here's both the edit and update methods in the <code>OrganizationEmailDomainController</code></p>
  <pre><code>/**
   * 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-&gt;path();
  
      $id = (int)explode('/', $path)[1];
  
      $emailDomain = OrganizationEmailDomain::find($id);
  
      return view('organizations.emaildomains.edit', ['email_domain' =&gt; $emailDomain-&gt;load('organization'), 'organizations' =&gt; $model::where('id', $emailDomain-&gt;organization_id)-&gt;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-&gt;update($request-&gt;all());
  
      $organization_id = (int)$request-&gt;all()['organization_id'];
  
      return redirect()-&gt;route('organizations.emaildomains.index', ['organization' =&gt; $organization_id])-&gt;withStatus(__(&quot;Org's email domain successfully updated.&quot;));
  }
  </code></pre>
  <p>and this is the model (notice I'm using a table with a different name than the expected by default - <code>protected $table = 'email_domains';</code>)</p>
  <pre><code>class OrganizationEmailDomain extends Model
  {
      protected $fillable = [
          'email_domain', 'organization_id'
      ];
  
      protected $table = 'email_domains';
  
      /**
       * Get the organization
       *
       * @return \Organization
       */
      public function organization()
      {
          return $this-&gt;belongsTo(Organization::class);
      }
  
  }
  </code></pre>