所以在这个项目中,我有主页,只显示当天发布的帖子。现在我需要当天的价格总和,为此我将其添加到我的价格求和代码中
在我的总和价格home.blade.php
中:
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->whereDate('created_at','=',$date)->sum('cijena') }}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Gotovina')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Virman')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
<th>NK: {{ Auth::user()->posts->where('placanje', 'NK')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
</tr>
</tfoot>
这个whereDate
方法来自我的HomeController
,这里是:
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);
}
我在web.php
的路线是:
Route::get('/home', 'HomeController@index')->name('home');
它返回给我的只是不存在Date的方法。有什么想法可以解决这个问题吗?
在您看来,您可以将一些行与以下内容一起使用:
Auth::user()->posts->where...
假设Auth::user()->posts
返回collection
用户帖子,但Auth::user()->posts()
返回query builder
实例。
幸运的是,laravel collection
定义了where()
方法,但它没有whereDate()
方法,而是在query builder
上定义了该方法,因此在您看来,您必须使用以下行:
Auth::user()->posts()->whereDate('created_at','=',$date)->sum('cijena')