php - Laravel 5.3 Eloquent - How to post data to multiple tables with a form?

459

So far I can only post data to 1 table with a form.

My controller:

public function store(Request $request, Role $role)
{   
    $role->fill($request->all());  

    $role->save();

    return redirect('/roles');
}

My view:

<div >
  <label for="name-input">@lang('role.name')</label>
  <input id="name-input" type="text" name="name" value="{{ isset($role) ? $role->name : '' }}" autofocus>
</div>

<div >
  <label for="description-input">@lang('role.description_optional')</label>
  <textarea id="description-input" name="description" >{{ isset($role) ? $role->description : '' }}</textarea>            
</div>

So it perfectly stores thename anddescription, but what if I wanted to store data to a second table?

333

Answer

Solution:

Something like this

$second = new SecondTableModel(); 
$second->fill($request->all());  
$second->save();

People are also looking for solutions to the problem: php - Add middleware after login in laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.