samedi 10 août 2019

Laravel - sum searched posts price

How can I sum price only for posts that I searched? I have a search page and search is for date, so I search posts by a date. And when I type date that I want I need to sum all posts prices that my search function find for that date. Here is my code.

This is my search in 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!');
});

And this is my search function in PagesController:

public function search(){
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        return view('search')->with('posts', $user->posts);
    }

And this is my search.blade.php with table footer where is should sum my posts price:

<tfoot>
   <tr>
      <th>UKUPAN IZNOS:&nbsp;&nbsp;&euro;</th>               
   </tr>
</tfoot>

but when I type this it sums me price for all posts, and I just need for the searched ones. Any suggestions?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire