Reputation: 87
I want to get the data from two different date range.How can i achive this?I have tried and got the data for starting date.
Controller
public function detail(Request $request,$id)
{
if($request->startdate) {
$advance =Advancepayment::where('date',$request->startdate)->get();
} else {
$advance = Advancepayment::all();
}
return view('admin.managesalary.detail',compact('advance'));
}
blade file
<form action="{{route('managesalary.detail',$user->id)}}"
method="GET"
class="form-horizontal"
>
<div class="card-body">
<h4 class="card-title">Search</h4>
<div class="form-group">
<!-- Date Picker -->
<div class="input-group date " id="startDate">
<strong>From</strong>
<input
type='date'
value="{{request()->startdate}}"
name="startdate"
class="form-control"
/>
</div>
<!-- Time Picker -->
<div class="input-group date" id="startTime">
<strong>To</strong>
<input type='date'
value="{{request()->enddate}}"
name="enddate"
class="form-control"
/>
</div>
</div>
</div>
<br><br>
<div class="border-top">
<div class="card-body">
<button type="submit" class="btn btn-success">Search</button>
<a href="{{route('managesalary.detail',$user->id)}}"
class="btn btn-md btn-danger"
>
Clear
</a>
</div>
</div>
</form>
route
Route::get('managesalary/detail/{id}',[
'as' => 'managesalary.detail',
'uses' => 'ManagesalaryController@detail'
]);
Upvotes: 0
Views: 251
Reputation: 6341
I have created the model scope
More about scopes
Code:
/**
* Scope a query to only include the last n days records
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWhereDateBetween($query,$fieldName,$fromDate,$todate)
{
return $query->whereDate($fieldName,'>=',$fromDate)->whereDate($fieldName,'<=',$todate);
}
And in the controller, if you are using the Carbon library for handling dates, add the Carbon Library to top
use Carbon\Carbon;
OR
use Illuminate\Support\Carbon;
To get the last 10 days record from now
$lastTenDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(10)->toDateString(),(new Carbon)->now()->toDateString() )->get();
To get the last 30 days record from now
$lastThirtyDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(30)->toDateString(),(new Carbon)->now()->toDateString() )->get();
So for your situation:
$recordResults = ModelName::whereDateBetween('filedName','2019-04-10','2019-04-12')->get();
Upvotes: 0
Reputation: 9693
Let's the date range is from last year 15/04 to this year 15/04, the code will be like
$from = date('2018-04-15');
$to = date('2019-04-15');
$advance = Advancepayment::whereBetween('date', [$from, $to])->get();
if there is condition the $to is empty you can put a conditional
public function detail(Request $request,$id)
{
$from = $request->input('startdate');
$to = $request->input('enddate');
if ( empty($to) && empty($from) ) {
$advance = Advancepayment::all();
} elseif ( empty($to) && ! empty($from) ) {
$advance = Advancepayment::where('date', $from)->get();
// or Advancepayment::where('date', '>', $from)->get(); depending upon your requirmeent
} else {
$advance = Advancepayment::whereBetween('date', [$from, $to])->get();
}
}
Upvotes: 3
Reputation: 1814
public function detail(Request $request,$id)
{
$startdate=$request->input('from');
$enddate=$request->input('to');
if(!empty($request->startdate) && !empty($enddate)) {
$advance =Advancepayment::whereBetween('date',[$startdate,$enddate])->get();
}
else if(!empty($request->startdate) && empty($enddate))
{
$advance =Advancepayment::where('date','>=',$request->startdate)->get();
}
else
{
$advance = Advancepayment::all();
}
return view('admin.managesalary.detail',compact('advance'));
}
Upvotes: 0