So I have this project and I almost finished it. I got an answer from my previous question for laravel carbon date and it's working great! It shows me all posts made today because of $from=$request->has('from')?$request->query('from'):Carbon::now()->subDays(1);
$to=$request->has('to')?$request->query('to'):Carbon::now()->endOfDay();
and all I need know is daterangepicker in frontend to choose any date I want, and when I choose that date I would see all posts made that day. Please help!
Here is my HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Post;
use DB;
use Auth;
use Carbon\Carbon;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
/*
public function index(Request $request){
// set default value for from is yesterday (you can set as per your requirement)
// set default value for to is today
$from=$request->has('from')?$request->query('from'):Carbon::now()->subDays(1);
$to=$request->has('to')?$request->query('to'):Carbon::now()->endOfDay();
$posts = Post::where('user_id', Auth::id())
->whereBetween('created_at',[$from,$to])
->orderBy('created_at', 'DESC')
->paginate(30);
return view('home')->with(['from'=>$from,'to'=>$to,'posts'=>$posts]);
}
}
Here is my home.blade.php
@extends('layouts.theme')
@section('content')
<style>
body{
margin: 0;
background-color: #EBEBEB;
}
</style>
<div class="mainl">
<div class="row">
<div class="columna">
<h1>{{ Auth::user()->name }}</h1>
<hr>
</div>
<div class="columns">
<a href="{{ route('logout') }}" id="logout"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('LOGOUT') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
@csrf
</form>
</div>
</div>
</br></br></br>
@if(count($posts)> 0)
<table>
<thead>
<tr>
<th>BR.KESICE</th>
<th>IME I PREZIME</th>
<th>BR.TELEFONA</th>
<th>POSAO</th>
<th>CIJENA</th>
<th>PLACANJE</th>
<th>POPUST</th>
<th>DATUM PREUZ.</th>
<th>DATUM IZDAV.</th>
<th>SMJENA</th>
<th>RADNIK</th>
<th>STATUS</th>
<th>IZMIJENI</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{$post->br_kesice}}</td>
<td>{{$post->ime}}</td>
<td>{{$post->br_telefona}}</td>
<td>{{$post->posao}}</td>
<td>{{$post->cijena}}</td>
<td>{{$post->placanje}}</td>
<td>{{$post->popust}}</td>
<td>{{$post->datum_preuz}}</td>
@if($post->status == 1)
<td>/</td>
@else
<td>{{$post->datum_izdav}}</td>
@endif
<td>{{$post->smjena}}</td>
<td>{{$post->radnik}}</td>
<td>
@if($post->status == 0)
<span class="label label-primary" id="statusdeaktivan">Deaktivan</span>
@elseif($post->status == 1)
<span class="label label-success" id="statusaktivan">Aktivan</span>
@elseif($post->status == 2)
<span class="label label-danger" id="statusdeaktivan">Rejected</span>
@else
<span class="label label-info" id="statusdeaktivan">Deaktivan</span>
@endif
</td>
@if($post->status == 3)
@else
<td><a href="posts/{{$post->id}}/edit" class="edit"><i class="far fa-edit"></i></a></td>
@endif
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->whereBetween('created_at',[$from,$to])->sum('cijena')}}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Kartica')->where('placanje', 'Gotovina')->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Kartica')->where('placanje', 'Virman')->sum('cijena')}}€</th>
</tr>
</tfoot>
</table>
<br><br>
{{ $posts->links() }}
@else
<p>Trenutno nema unosa.</p>
@endif
</div>
@endsection
Any solutions to code daterangepicker in frontend so I can choose any date I want to see posts from that date?
You can include datetimepicker in your application make two inputs: one for from input and the another one for to input and make filter button when clicked ,send the two value in your controller format the dates
$from=date('Y-m-d',strtotime($request->from));
$to=date('Y-m-d',strtotime($request->to))
$posts = Post::where('user_id', Auth::id())
->whereBetween('created_at',[$from,$to])
->orderBy('created_at', 'DESC')
->paginate(30);