Laravel在成功后附加数据ajax


Laravel append data after success ajax

我有这个div:

@foreach($bids as $b)
<div class="bids notice notice-lg">
    <strong>{{$b->user->name}}</strong> siūlo <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span>
</div>
@endforeach

这是我的 AJAX:

$(function(){
$('#placeBid').on('submit',function(e){
    $.ajaxSetup({
        headers: {
            'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
        }
    });
    e.preventDefault(e);
        if($("#jsBidPrice").val() <= $(".minBid").val()){
        $.ajax({
        method:"POST",
        url: $("#placeBid").attr("action"),
        data:$(this).serialize(),
        dataType: 'text',
        success: function(data){  
           $('.minBid').val(0);
           $("#bidSubmit").attr("disabled", true);
        },
        error: function(data){
            console.log("request failed");
        }
    })
    }
        else {
            alert('Too low.');
        }
    });
});

如何在我的div #bids 后附加新提交的数据?这就是我现在正在尝试的 - 我正在尝试创建新的 DIV 并将其放置在每个值中。喜欢这个:

var html = '<strong>{{$b->user->name}}</strong>';
html += 'siūlo <span style="font-size: 18px" class="label label-success">';
html += '54044 €</span>';
$('.bidsA').append(html);

我把这个div 放在出价div 上。所以 HTML 看起来像这样:

@foreach($bids as $b)
<div class="bidsA notice notice-lg">
</div>

<div class="bids notice notice-lg">
    <strong>{{$b->user->name}}</strong>siūlo <span style="font-size: 18px" class="label label-success">{{$b->bid}} €</span>
</div>
@endforeach

当然,我undefined variable $b如何正确地将其从 AJAX 包含在 HTML 中?

编辑

public function store(BidRequest $request, $id)
{   
     $product = Product::where('id', $id)->first();
     $bid = new Bid([
        'product_id' => $product->id,
        'bid'   => $request->bid,
     ]);
     Auth::user()->bid()->save($bid);
    }
}

您可以返回视图并刷新包含出价的部分。

在控制器中,将$bids返回给view,它将构建包含所有出价div的html:

public function store(BidRequest $request, $id)
{   
    ....
    $bids = Bid::orderBy('bid', 'DESC')->paginate(10);
    return view('products.show', compact('bids'));
}

然后在JS中,您将收到将其附加到父容器中的html:

success: function(data){  
    $('.bids_container').replaceWith(data);
}

希望这有帮助。