我有一个控制器,该控制器根据请求将数据发送到我的Ajax函数,它根据从ajax获得的值搜索值,然后根据控制器中的查询将搜索结果发送到ajax函数。我如何使用数据,以下是当我警告数据以这种形式[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
AJAX功能
$('#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) {
alert(pasts);
// document.getElementById("expiry_label").innerHTML = "Trial End Date: " + result;
}
});
}
});
控制器
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));
}}
答案 0 :(得分:0)
我相信您想在AJAX函数中以pasts['pasts']
的形式访问数据,因为对array('pasts' => $pasts)
的PHP调用正在使用存储在键“过去”下的数据创建一个关联数组。即
$('#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;
}
});
}
});