Laravel-选择日期以使用daterangepicker查看该日期后的帖子

时间:2019-07-16 18:06:28

标签: laravel date range picker

所以我有这个项目,我几乎完成了。我从之前的laravel carbon date问题中得到了答案,并且效果很好!它向我显示了由于$from=$request->has('from')?$request->query('from'):Carbon::now()->subDays(1); $to=$request->has('to')?$request->query('to'):Carbon::now()->endOfDay();而今天发布的所有帖子,我只需要知道前端的daterangepicker即可选择所需的任何日期,当我选择该日期时,我就会看到当天的所有帖子。请帮忙!

这是我的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]);

        }
}

这是我的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:&nbsp;&nbsp;{{ Auth::user()->posts->whereBetween('created_at',[$from,$to])->sum('cijena')}}&euro;</th>
                    <th>KARTICA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Kartica')->sum('cijena')}}&euro;</th>
                    <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Kartica')->where('placanje', 'Gotovina')->sum('cijena')}}&euro;</th>
                    <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Kartica')->where('placanje', 'Virman')->sum('cijena')}}&euro;</th>
                </tr>
            </tfoot>
        </table>
        <br><br>
        {{ $posts->links() }}
        @else
            <p>Trenutno nema unosa.</p>
        @endif
    </div>
@endsection

任何在前端对daterangepicker进行编码的解决方案,以便我可以选择要从该日期开始查看帖子的任何日期?

1 个答案:

答案 0 :(得分:0)

您可以在应用程序中包括datetimepicker 进行两个输入:一个用于输入,另一个用于输入 并在单击时制作过滤器按钮,发送两个值 在您的控制器中 格式化日期

$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);