Laravel 5.2更新同一页上的Eloquent主细节

6
这里有两个模型,分别是blade页面和控制器,用于在Laravel 5.2中实现主从页面。我想知道如何将详细信息部分更新回数据库。
1)主模型
namespace App;

use Illuminate\Database\Eloquent\Model;

class Order  extends Model
{
    protected $fillable = [
        'id',
        'name',
        'created_at',
        'updated_at'
    ];

    public function detail()
    {
        return $this->hasMany('App\OrderDetail','fid','id');
    }   



    public function getDetailCountAttribute()
    {
        return $this->hasMany('App\OrderDetail','fid','id')->count();
    }       


    public static function boot()
    {
        parent::boot();    

        // cause a delete of a product to cascade to children so they are also deleted
        static::deleted(function($order)
        {
            $order->detail()->delete();
        });
    }    
}

2) Detail Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderDetail extends Model
{
    protected $fillable = [
        'id',
        'oid',
        'item',
        'total',
        'created_at',
        'updated_at'
    ];
}

3) The master detail page (partial), in OrderDetail part, jQuery applied to dynamically add items in the table.

<body>
    <h1>Order Edit</h1>
        {!! Form::model($order,['method' => 'PATCH','route'=>['order.update',$order->id]]) !!}
    <div class="form-group">
        ORDER ID:{{$order->id}}
        <br /><br />
    </div>
    <div class="form-group">
        {!! Form::label('order Name', 'ORDER NAME:') !!}
        {!! Form::text('name',$order->name,['class'=>'form-control']) !!}
    </div>
    <div class="form-group">
     <!--order detail part start-->   

    <br />
    {{ Form::select('productInput', $product,'',['ID'=>'productInput']) }}

    {{ Form::number('productQtyInput','',['class'=>'form-control','style'=>'width:250px','ID'=>'OrderQtyInput']) }}


    {{ Form::button('Add',['onclick'=>'product_add();']) }}

    <br />
    <table class="table table-striped table-bordered table-hover" style="width:30%;" border=1 id="TblOrderList" >
        <thead>
             <tr class="bg-info" id="ingHEADER">
                 <th width=2%>PRODUCTID</th>
                 <th width=5% >PRODUCT</th>
                 <th width=5%>QTY</th>
                 <th width=5% colspan=2>COMMAND</th>
             </tr>
        </thead>
        <tbody>
        </tbody>
    </table>        
     <!--order detail part end-->   
    </div>
    <div class="form-group">
        {!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}
    </div>
    {!! Form::close() !!}
</body>

<script type="text/javascript">
function product_add(){
    var ingID =$( "#productInput").val();
    var ingName =$( "#productInput option:selected").text();
    var ingQty =$("#productQtyInput").val();

    var rowCount = $('#TblOrderList .ingData').length;
    rowCount++;


    var outHTML =
        '<tr class="ingData" id="ingRow' + rowCount + '">'
            + '<td><div id="ingID' + rowCount + '">' + ingID + '</div></td>'
            + '<td><div id="ingName' + rowCount + '">' + ingName + '</div></td>'
            + '<td><input type="number" id="ingQTY' + rowCount + '" value="' + ingQty + '"/></td>'
            + '<td>'
                + '<button class="OrderREMOVE" type="button">DELETE</button>'
                + '<button class="OrderUP" type="button">up</button>'
                + '<button class="OrderDOWN" type="button">down</button>'
            + '</td>'
        + '</tr>';

    $('#TblOrderList tr:last').after(outHTML);
    $('#ingUNIT' + rowCount)
        .find('option[value="' + ingUnit + '"]')
        .prop('selected', 'selected');

}
</script>

4) Finally the update function in OrderController, currently it can update the master part. But I am not sure how to loop each detail and update them in same function.

public function update(Request $request, $id)
{
    //
   $orderUpdate = $request->all();
   $order = Order::find($id);
   $order->update($orderUpdate);


   return redirect('order');        
}

1个回答

6
您有两个选项来实现这个功能:
  1. 要么您为每个OrderDetail项目发出一个请求(在这种情况下,您需要通过ajax发送POST值,并且可以保留控制器代码)
  2. 要么您创建一个输入数组,一次性提交所有数据,并在服务器上处理这些输入。

如果选择第二个选项,则需要在输入名称中添加[]以将其设置为数组。 实际上,您的输入根本没有名称,因此您的控制器永远无法接收该输入的值,因为它们不存在于POST变量中。

所以您需要像这样:

<input type="number" name="qty[]" id="ingQTY' + rowCount + '" value="' + ingQty + '"/>

注意 name="qty[]" 的用法。

在控制器方面,您只需要:

public function update(Request $request, $id)
{
    // Update the order
    $orderUpdate = $request->all();
    $order = Order::find($id);
    $order->update($orderUpdate);

    // Go through all qty (that are related to the details, and create them)
    foreach ($postValues['qty'] as $qty) {

        $order->detail()->create([ 
            'oid' => $order->id,
            'total' => $qty,
        ]);
    }
    return redirect('order');       
}

这应该能让你了解你需要什么。当然,你应该提交订单详细ID并检查控制器是否存在该ID,并且只有在存在该值时才更新详细信息。如果不存在,则创建详细信息。

我还认为您在订单详细信息中缺少一些输入(在JavaScript部分),因为该模型具有“项”属性,而您似乎没有为该列创建输入。


谢谢,是的,我只是简要介绍了我的代码尝试做什么。你的回答给了我下一步实现的方向。 - mememoremore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接