Laravel - 方法,其中日期不存在



所以在这个项目中,我有主页,只显示当天发布的帖子。现在我需要当天的价格总和,为此我将其添加到我的价格求和代码中

在我的总和价格home.blade.php中:

<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->where('placanje', 'Kartica')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
    <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Gotovina')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
    <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Virman')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
    <th>NK:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'NK')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</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')

最新更新