我的路线如下;
Route::post ('/sepetim', 'ShoppingCartController@addStock');
我的控制器如下;
public function addStock(Request $request) {
$data = new ShoppingCartDetail();
$data->cart_id = $request->input('cart_id');
$data->stock_id = $request->input('stock_id');
$data->price = 1;
$data->save();
}
我的ajax如下:
$.ajax({
type: 'POST',
url: '/sepetim',
data: {
'_token': $('input[name="_token"]').val(),
'name': $('input[name=name]').val(),
'cart_id': $('input[name=cart_id]').val(),
'stock_id': id
},
success: function(data) {
// alert("Başarılı bir şekilde eklendi.");
},
});
我想用Ajax更新div。
@foreach($all_values_list as $row)
<div class="sepeturunalan row d-flex align-items-center">
<div class="col-lg-1 col-sm-1 col-1">
<img src="http://vomsis.com/uploads/thumb_{{$row['imageable_name']}}" class="img-fluid">
</div>
<div class="col-lg-4 col-sm-4 col-12 pl">
<a href="#" title="">
{{$row['stock_name']}}
</a>
</div>
<div class="col-lg-2 col-sm-2 col-2 text-center">
<span class="para">{{number_format($row['without_tax'] , '2' , ',' , '.')}}</span>
</div>
<div class="col-lg-1 col-sm-1 col-1 plr text-center">
<span class="para">{{number_format($row['new_price'] - $row['without_tax'] , '2' , ',' , '.')}} TL</span> <br/>
<span class="fw">(%{{$row['tax_ratio']}})</span>
</div>
<div class="col-lg-2 col-sm-2 col-2 text-center">
<div class="input-group adet">
<span class="input-group-btn">
<button type="button" id="{{$row['id'].'u'}}" class="btn-number" onclick="buttons_click_minus({{$row['id']}})" data-type="minus" data-field="quant[1]">
<i class="fa fa-minus"></i>
</button>
</span>
{{ csrf_field() }}
{{ Form::hidden('cart_id', $cart_id) }}
{{ Form::hidden('stock_id', $row['id']) }}
<input type="text" id="{{$row['id']}}" name="name" class="input-number" onchange="input_onchange(this.id)" value="{{$row['quantity']}}" min="1" max="25">
<span class="input-group-btn">
<button type="button" type="submit" id="{{$row['id']}}" class="btn-number" onclick="buttons_click_plus(this.id)" data-type="plus" data-field="quant[1]">
<i class="fa fa-plus"></i>
</button>
</span>
</div>
</div>
<div class="col-lg-2 col-sm-2 col-2 text-right">
<span class="para fw6">{{number_format($row['quantity'] * $row['new_price'] , '2' , ',' , '.')}} TL</span> <br/>
<span>
{!! Form::open(['url' => '/sepetim/'.$cart_id .'/'.$row['id'], 'method' => 'delete']) !!}
<button type="submit" class="sepsil">
<i class="fa fa-trash-o"></i> Sil
</button>
{!! Form::close() !!}
</span>
</div>
<div class="col-lg-6 col-sm-6 col-6">
<p class="aciklama">
<i class="fa fa-info-circle renk"></i>
<span class="para">100TL </span> üzeri kargo bedava! kampanyasına dahil ürün
</p>
</div>
</div>
@endforeach
以上代码正在注册数据库。 我的问题:如何使用ajax显示存储在数据库中的数据 - Laravel? enter image description here
单击按钮时Ajax正在工作。注册到数据库。我怎么才能表现出来。
答案 0 :(得分:0)
在$data->save();
之后,执行return response()->json(["data" => $data]);
之类的操作,$data
通过success: function(data)
将json
作为data.data
对象传递给public function addStock(Request $request) {
...
return response()->json(["data" => $data]);
}
:< / p>
控制器功能(PHP):
...
success: function(data) {
console.log(data.data); // (May want to change your PHP return value to something other than data)
},
成功函数(JS):
data
要在页面中实际显示返回的addStock
,您需要通过success
方法返回一些HTML,或者在public function addStock(Request $request){
...
$renderedHtml = view("example")->with(["data" => $data])->render();
return response()->json(["data" => $data, "renderedHtml" => $renderedHtml]);
}
函数中构建一些HTML:
控制器功能(PHP)
...
success: function(data) {
$("#someDiv").html(data.renderedHtml);
// OR
var renderedHtml = "<div>";
renderedHtml += "<span>$" + data.data.price + "</span>";
// etc etc
renderedHtml += "</div>";
$("#someDiv").html(renderedHtml);
},
成功函数(JS):
RewriteEngine on
#redirect and rewrite url from http://project2.local/camera?camera_id=1 to http://project2.local/camera/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /camera\.php\?camera_id=([^\&\s]+)
RewriteRule ^/?camera\.php$ /camera/%1? [L,R=301]
RewriteRule ^camera/([^/]*)$ /camera.php?camera_id=$1 [L]
#remove. php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [NC,L]