Laravel-显示所有使用Carbon的帖子

时间:2019-07-16 16:05:44

标签: php laravel php-carbon

所以我在项目中使用了carbon,现在它显示了我今天发表的所有帖子,这对我来说还可以。但是现在我需要查看过去几天的帖子,我该如何使用碳呢?在我看来,过去几天我想要类似href链接的东西home.blade.php

这是我的家用碳纤维家用控制器

<?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()
    {
        $date = new Carbon(request('date'));
        $posts = Post::where('user_id', Auth::id())
                ->whereDate('created_at','=',$date)
                ->orderBy('created_at', 'DESC')
                ->paginate(30); //add {{ $posts->links() }} if paginate is enabled
        return view('home', compact('date', $date))->with('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()->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
                    <th>KARTICA:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Kartica')->sum('cijena')}}&euro;</th>
                    <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Kartica')->where('placanje', 'Gotovina')->sum('cijena')}}&euro;</th>
                    <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts()->whereDate('created_at','=',$date)->where('placanje', 'Kartica')->where('placanje', 'Virman')->sum('cijena')}}&euro;</th>
                </tr>
            </tfoot>
        </table>
        {{ $posts->links() }}
        @else
            <p>Trenutno nema unosa.</p>
        @endif
    </div>
@endsection

有解决方案吗?我尝试过但没有成功。

2 个答案:

答案 0 :(得分:0)

发送2个查询参数fromto,并使用 whereBetween 显示指定日期范围内的帖子

您的网址将类似于:

  

/ posts?from = 2019-07-15&to = 2019-07-16

在您的web.php中路由

Route::get('/posts','HomeController@index')->name('posts');

在您的控制器中:

public function index(){

 // 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]);

 }

从前端使用daterangepicker会更好。

答案 1 :(得分:0)

只需将此行设为whereDate('created_at','=',$date) 像这样 whereDate('created_at','<=',$date)

相关问题