【发布时间】:2017-06-08 07:56:47
【问题描述】:
我有一段 View 代码,其中包含来自对 Ajax 的“.get”请求的数据。 .get 请求将一些 PHP 代码传递给控制器,该控制器从查询中检索结果并将它们存储在 JS 对象“bookingObj”中。
问题是,我想使用一些由查询检索并存储在此对象中的 id 作为参数,用于路由到另一个 laravel 函数“edit”。目前,当我尝试将这些变量连接到刀片辅助函数中时,我没有得到它们的实际值,只是逐字逐句地得到变量名。
例如,在我的代码中:
<a href="{{ route("bookings.edit", [' + bookingObj.id + ',' + bookingObj.tour_id + ',' + bookingObj.datedtour_id + ']) }}">
// 在 chrome 检查器中返回这个
<a href="http://localhost/laravel/bookings/ + bookingObj.id + /edit/ + bookingObj.tour_id + / + bookingObj.datedtour_id + ">Edit</a>
我希望看到整数(来自数据库的 ID)代替“bookingObj”属性。
任何人都知道我是否必须使用任何转换函数来显示此内容,或者可能愿意尝试解释问题所在?
非常感谢!
这是我的其余代码:
$.get(url + variable, function(data){
console.log('Call complete');
//success data
$('tr#bookingstable').remove();
$.each(data, function(index, bookingObj){
var startdate = new Date(bookingObj.datedtourstartdate.replace(/-/g,"/"));
var enddate = new Date(bookingObj.datedtourenddate.replace(/-/g,"/"));
$('table').append(
'<tr id="bookingstable"><td scope="row"><a href="{{ route("bookings.show", $booking->id) }}">' + bookingObj.name + '</a></td><td scope="row">' + bookingObj.size + '</td><td scope="row">' + bookingObj.tour_name + '</td><td scope="row">' + bookingObj.category_name + '</td><td scope="row">' + bookingObj.continent_name + '</td><td scope="row">' + dateFormat(startdate, "dd.mm.yyyy") + ' - ' + dateFormat(enddate, "dd.mm.yyyy") + '</td><td scope="row"><li class="tiny success button"><a href="{{ route("bookings.edit", [' + bookingObj.id + ',' + bookingObj.tour_id + ',' + bookingObj.datedtour_id + ']) }}">Edit</a></li></td><td scope="row"><form method="post" name="_token" value="<?php echo csrf_token(); ?>" action="{{ route("bookings.destroy", ["id" => $booking->id]) }}">{!! csrf_field() !!}<input type="hidden" name="_method" value="DELETE"><input type="hidden" name="booking_id" value="{!! $booking->id !!}"><input type="submit" class="tiny alert button" value="Delete"></form></td></tr>'
); // end append
}); // end each
}); // end get
谢谢,
问候,
罗伯特
英国伦敦
【问题讨论】:
-
一般来说,对于这样的事情,在刀片模板中硬编码路线会更容易。
-
我已经在stackoverflow.com/questions/27634285/…找到了解决这个问题的方法
标签: javascript php ajax laravel