【发布时间】:2019-08-06 20:08:46
【问题描述】:
所以在这个项目中,我的主页只显示当天发布的帖子。现在我需要那天的价格总和,为此我将它添加到我的价格总和代码中
在我的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');
它返回给我的只是那个方法 whereDate 不存在。任何想法如何解决这个问题?
【问题讨论】:
-
你认为错误是你忘记了每个
->posts后面的括号:你应该使用Auth::user()->posts()->... -
要获取用户ID,请尝试
Auth::user()->id -
是的,我忘了括号..这就是你恐慌时得到的..谢谢老兄。如果你能帮助我,我还有一个问题,我应该发布一个新问题还是我们可以在这里做?