【发布时间】:2019-11-24 16:32:15
【问题描述】:
所以伙计们,我在我的项目中添加了分页,我快完成了,顺便说一句,女巫是我的第一个项目,我现在需要做的就是通过 created_at 设置分页。所以我需要发布帖子从同一天在同一页面链接。现在它在一页上显示不同日子的帖子。之后我只需要显示当天的价格总和。如果你知道请帮助我,这将是我作为学生的第一个项目,我还在学习。谢谢!!
这是我的 HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Post;
use DB;
use Auth;
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()
{
$posts = Post::where('user_id', Auth::id())->orderBy('created_at', 'DESC')
->paginate(3);
return view('home')->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: {{ Auth::user()->posts->sum('cijena')}}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Gotovina')->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Virman')->sum('cijena')}}€</th>
</tr>
</tfoot>
</table>
{{ $posts->links()}}
@else
<p>Trenutno nema unosa.</p>
@endif
</div>
@endsection
我在堆栈溢出中从过去的问题中找到了一些东西,它正在工作,但我有一个问题,所以
public function index()
{
$posts = Post::where('user_id', Auth::id())->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->get();
return view('home')->with('posts', $posts);
}
当我添加此 ->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->get(); 时,它会显示过去 10 天的所有帖子,但在那 10 天之后什么都没有,我可以使用它,而是按所有天数排序,而不是设置最长天数吗?
我还将此代码添加到我的价格总和中,它正在工作,但正如我所说的过去 10 天
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Gotovina')->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Virman')->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))->sum('cijena')}}€</th>
</tr>
</tfoot>
再次感谢您,如果您能帮我解决这个问题,我将不胜感激。这是我的第一个项目,我对此感到非常高兴。
【问题讨论】:
标签: laravel sorting date pagination