簡體   English   中英

Laravel 5.3 Eloquent-如何使用表單將數據發布到多個表?

[英]Laravel 5.3 Eloquent - How to post data to multiple tables with a form?

到目前為止,我只能使用表單將數據發布到1個表中。

我的控制器:

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

    $role->save();

    return redirect('/roles');
}

我的觀點:

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

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

因此,它完美地存儲了namedescription ,但是如果我想將數據存儲到第二張表怎么辦?

像這樣

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM