Javascript 在Ajax中使用值控制器
我有一个控制器,它根据Ajax函数的请求将数据发送到Ajax函数,它根据从Ajax中获得的内容搜索一个值,然后根据控制器中的查询将搜索结果发送到Ajax函数。我如何使用数据,下面是我希望如何使用php表单中的数据的一个示例,当我提醒数据以这种形式出现时Javascript 在Ajax中使用值控制器,javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,我有一个控制器,它根据Ajax函数的请求将数据发送到Ajax函数,它根据从Ajax中获得的内容搜索一个值,然后根据控制器中的查询将搜索结果发送到Ajax函数。我如何使用数据,下面是我希望如何使用php表单中的数据的一个示例,当我提醒数据以这种形式出现时[object object] CONSOLE.LOG带来了这一点 {…} pasts: (3) […] 0: {…} booking_id: "260618-00017" booking_status: "BOOKED" channel
[object object]
CONSOLE.LOG带来了这一点
{…}
pasts: (3) […]
0: {…}
booking_id: "260618-00017"
booking_status: "BOOKED"
channel_id: 4
check_in: "2018-06-26"
check_out: "2018-06-27"
comments: null
created_at: "2018-06-26 15:34:28"
currency_code: null
guest: Object { id: 17, first_name: "msklsd", last_name: "klld;sld", … }
guest_id: 17
id: 15
is_staying: 0
made_by: "Room Hub"
number_of_guests: 7
paid_status: null
property_id: 11
reservation_payment_id: null
stay_id: null
updated_at: "2018-06-26 15:34:28"
__proto__: Object { … }
PHP表单示例
@foreach($pasts as $past)
<tr class="bg-white div-hover room-reserve" id="reserved" data-id="{{$past->id}}" style="cursor: pointer;">
<td>{{$past->guest['first_name']}} {{$past->guest['last_name']}}</td>
<td><img src="/{{$past->channel['icon']}}" alt="" style="margin:0;padding:0; width:50px;height:50px; border-radius:50%;"></td>
{{-- <td>{{$reservation->property['name']}}</td> --}}
<td>@if(isset($past->reservationDetails[0]->number_of_rooms))@if($past->reservationDetails[0]->number_of_rooms >1)
{{$past->reservationDetails[0]->number_of_rooms}} {{$past->reservationDetails[0]->room_category[0]->description}}s
@else {{$past->reservationDetails[0]->room_category[0]->description}} @endif @endif
</td>
<td>{{$past->number_of_guests}}</td>
<td>{{$past->channel['name']}}</td>
<td>{{date('d/m/Y',(strtotime($past->check_in)))}} to {{date('d/m/Y',(strtotime($past->check_out)))}}</td>
<td>
<button class="btn btn-danger btn-sm p-0 px-1 reject-booking-request" data-reservation-id="">Reject</button>
<button class="btn btn-success btn-sm p-0 px-1 accept-booking-request" data-reservation-id="">Accept</button>
</td></tr>@endforeach
控制器
public function searchPastReservations(Request $request){
$ts = strtotime('today');
$today= date('Y-m-d', $ts);
$defaultPropertyId = (session()->get('default_property'))->id;
$property_id = $defaultPropertyId ;
$searchValue = $request->searchValue;
// if (isset)
if ($searchValue && !empty($searchValue)){
$pasts = Reservation::with("Guest")->where('booking_status','BOOKED')
->where('property_id', $property_id)->where('check_out', '<', $today)
->whereHas('guest', function($q) use($searchValue) {
// Query the name field in status table
$q->where('first_name', 'like', "%" . $searchValue ."%")->orWhere('last_name', 'like', "%" . $searchValue ."%");
// '=' is optional
})->orderBy('check_in', 'asc')->get();
return response()->json(array('pasts' => $pasts));
}}
公共功能预订(请求$Request){
$ts=标准时间(“今天”);
$today=日期('Y-m-d',$ts);
$defaultPropertyId=(session()->get('default_属性'))->id;
$property\u id=$defaultPropertyId;
$searchValue=$request->searchValue;
//如果(isset)
如果($searchValue&!empty($searchValue)){
$pasts=预订::与(“客人”)->其中('booking_status','BOOKED')
->where('property_id',$property_id)->where('check_out',”我相信您希望在AJAX函数中以pasts['pasts']
的形式访问数据,因为PHP调用array('pasts'=>$pasts)
正在创建一个关联数组,其中数据存储在键“pasts”下
使用console.log而不是alert,然后查看输出内容那么alert(pasts);
向您展示了什么?我建议使用console.log(pasts);
来代替,因为您将在控制台中看到实际对象。请重新检查我刚才添加console.log输出的问题
public function searchPastReservations(Request $request){
$ts = strtotime('today');
$today= date('Y-m-d', $ts);
$defaultPropertyId = (session()->get('default_property'))->id;
$property_id = $defaultPropertyId ;
$searchValue = $request->searchValue;
// if (isset)
if ($searchValue && !empty($searchValue)){
$pasts = Reservation::with("Guest")->where('booking_status','BOOKED')
->where('property_id', $property_id)->where('check_out', '<', $today)
->whereHas('guest', function($q) use($searchValue) {
// Query the name field in status table
$q->where('first_name', 'like', "%" . $searchValue ."%")->orWhere('last_name', 'like', "%" . $searchValue ."%");
// '=' is optional
})->orderBy('check_in', 'asc')->get();
return response()->json(array('pasts' => $pasts));
}}
$('#search-btn').click(function(e){
e.preventDefault();
e.stopPropagation();
let search = $('#search').val();
if (search != '') {
$.ajax({
type: 'post',
url: '{{action('ReservationController@searchPastReservations')}}',
data: {searchValue: search},
// dataType: 'json',
success: function (pasts) {
console.log(pasts['pasts']);
// document.getElementById("expiry_label").innerHTML = "Trial End Date: " + result;
}
});
}
});