我已經構建了一個laravel應用程序。我想用日期範圍搜索數據。在這裏,我使用控制器,並在調用控制器後,數據被檢索並重定向到相同的頁面。這裏有類似頁面加載的情況。但我不想加載整個頁面。而不是它,如果我想使用ajax調用控制器只會調用特定的控制器,並且沒有重定向。我如何使用Ajax做到這一點?如何調用ajax從數據庫檢索數據
這裏是我的控制器:
public function reportByDate(Request $request)
{
$revenue = DB::table('fees')
->join('courses', 'courses.id', '=', 'fees.course_id')
->join('users', 'courses.id', '=', 'users.course_id')
->where('users.completed_status', '=', 1)
->sum('fee');
$startDate = $request->Input(['sdate']);
$finshDate=$request->Input(['edate']);
$input = DB::table('fees')->join('courses', 'fees.course_id', '=','courses.id')
->join('users', 'users.course_id', '=', 'courses.id')
->where('users.completed_status', 1)
->whereBetween('users.date', [$startDate, $finshDate])
->groupBy('fees.course_id')
->selectRaw('SUM(fee) as fee, courses.name')
->get();
return view('revenueDetails',['revenue'=>$revenue],['input'=>$input]);
}
下面是這個視圖頁面:
@extends('layouts.master')
@section('title')
Revenue Details
@endsection
@section('content')
<div class="container">
<h3> Revenue Details </h3>
</br>
</br>
{!! Form::open(array('route' => 'reportByDate','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<div class="form-group">
<label> Start Date</label>
<input type="date" name="sdate" class="form-control" >
</div>
<div class="form-group">
<label> End Date</label>
<input type="date" name="edate" class="form-control" >
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
<h4>Total Revenue: {{$revenue}}</h4></br>
</br>
<table class="table table-striped table-bordered" id="example">
<thead>
<tr>
<td>Serial No</td>
<td>Course Name</td>
<td>Course Revenue</td>
</tr>
</thead>
<tbody>
{{-- {{dd($input)}} --}}
<?php $i=1; ?>
@foreach($input as $row)
<tr>
<td>{{$i}}</td>
<td>{{$row->name}}</td>
<td>{{$row->fee}}</td>
</tr>
<?php $i++; ?>
@endforeach
</tbody>
</table>
<li><a href="{{ route('userDetails') }}">See the User Details</a></li>
</div>
@endsection