Laravel - 总和搜索帖子价格

如何仅对我搜索过的帖子求和?我有一个搜索页面,搜索是为了日期,所以我按日期搜索帖子。当我输入我想要的日期时,我需要总结我的搜索功能为该日期找到的所有帖子价格。这是我的代码。


这是我的搜索web.php:


Route::get('/search', 'PagesController@search');


Route::post('/search',function(){

    $q = Input::get ( 'q' );

    $post = auth()->user()->posts()->where('ime','LIKE', '%'.$q.'%')->get();

    if(count($post) > 0)

        return view('search')->withDetails($post)->withQuery ( $q );

    else return view ('search')->withMessage('Nema rezultata Vaše pretrage. Probajte ponovo!');

});

这是我的搜索功能PagesController:


public function search(){

        $user_id = auth()->user()->id;

        $user = User::find($user_id);

        return view('search')->with('posts', $user->posts);

    }

这是我search.blade.php的表页脚在哪里应该总结我的帖子价格:


<tfoot>

   <tr>

      <th>UKUPAN IZNOS:&nbsp;&nbsp;{{ Auth::user()->posts()->sum('cijena') }}&euro;</th>               

   </tr>

</tfoot>

但是当我输入它时,它会汇总所有帖子的价格,而我只需要搜索到的帖子。有什么建议?


一只斗牛犬
浏览 139回答 1
1回答

holdtom

...但是当我输入它时,它总结了我所有帖子的价格,我只需要搜索到的这是因为您认为有这条线:<th> ... {{ Auth::user()->posts()->sum('cijena') }} ... </th>这是直接执行不同的查询以获取cijena. 因此,无论您是否限制结果,这都会使输出保持相同的值。这个不同的查询对其余的查询值有任何影响。您可以做的是在主查询中计算该值并将其返回到视图:Route::post('/search', function () {&nbsp; &nbsp; $q = Input::get('q');&nbsp; &nbsp; $posts = auth()->user()->posts()->where('ime', 'LIKE', '%' . $q . '%')->get();&nbsp; &nbsp; if (count($posts) > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $sum = $posts->sum('cijena'); // <---&nbsp; &nbsp; &nbsp; &nbsp; return view('search')->withDetails($posts)->withTotal($sum);&nbsp; &nbsp; }&nbsp; &nbsp;//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('search')->withMessage('Your error message goes here!');&nbsp; &nbsp; }});所以现在您可以访问$total刀片文件中的一个额外变量:<th> ... {{ $total) }} ... </th>此外,无需为同一操作定义两条路线,您可以通过一种简单的方法减少所有路线。此外,您不应从前端执行查询。请执行以下操作:# web.phpRoute::get('/search', 'PagesController@search');然后在您的控制器中:# PageController.phpuse Illuminate\Http\Request;// ...public function search(Request $request){&nbsp; &nbsp; $posts = auth()&nbsp; &nbsp; &nbsp; &nbsp; ->user()&nbsp; &nbsp; &nbsp; &nbsp; ->posts()&nbsp; &nbsp; &nbsp; &nbsp; ->when($request->has('q'), function ($q) { // first check if there is a query&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $q->where('ime', 'LIKE', '%' . request('q') . '%'); // if so, apply filter&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; ->get();&nbsp; &nbsp; if (count($posts) > 0) // checking if there is enough posts..&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $sum = $posts->sum('cijena'); // if so get the sum of 'cijena'&nbsp; &nbsp; &nbsp; &nbsp; return view('search')->withDetails($posts)->withTotal($sum);&nbsp; &nbsp; }&nbsp; &nbsp;//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('search')->withMessage('Your error message goes here!');&nbsp; &nbsp; }}更新这一行是引发错误的那一行:<p class="searchp">Rezultati vaše pretrage <b> {{$q}} </b>: </p>这是因为我没有包含$q变量。只需将其附加到您的回复中,以备不时之需:// ...return view('search')->withDetails($posts)->withTotal($sum)->with('q', request('q'));// ...
打开App,查看更多内容
随时随地看视频慕课网APP