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: {{ Auth::user()->posts()->sum('cijena') }}€</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?
... but when I type this it sums me price for all posts, and I just need for the searched ones
This is because you have this line in your view:
<th> ... {{ Auth::user()->posts()->sum('cijena') }} ... </th>
This is executing a different query directly to get the total sum of the cijena. So, regardless of if you constraint your results or not this will keep output the same vale. This different query has any impact on the rest of the queried values.
What you could do is to calculate that value in the main query and return it to the view:
Route::post('/search', function () {
$q = Input::get('q');
$posts = auth()->user()->posts()->where('ime', 'LIKE', '%' . $q . '%')->get();
if (count($posts) > 0)
{
$sum = $posts->sum('cijena'); // <---
return view('search')->withDetails($posts)->withTotal($sum);
} // ^^^^^^^^^^^^^^^^^
else
{
return view('search')->withMessage('Your error message goes here!');
}
});
So now you'll have access to an extra variable $total in your blade file:
<th> ... {{ $total) }} ... </th>
Also, there is no need to define two routes for the same operation, you could reduce all that in one simple method. Additional, you shouldn't execute queries from your front-end. Do as follows:
# web.php
Route::get('/search', 'PagesController#search');
Then in your controller:
# PageController.php
use Illuminate\Http\Request;
// ...
public function search(Request $request)
{
$posts = auth()
->user()
->posts()
->when($request->has('q'), function ($q) { // first check if there is a query
return $q->where('ime', 'LIKE', '%' . request('q') . '%'); // if so, apply filter
})
->get();
if (count($posts) > 0) // checking if there is enough posts..
{
$sum = $posts->sum('cijena'); // if so get the sum of 'cijena'
return view('search')->withDetails($posts)->withTotal($sum);
} // ^^^^^^^^^^^^^^^^
else
{
return view('search')->withMessage('Your error message goes here!');
}
}
Update
This line is the one that throws the error:
<p class="searchp">Rezultati vaše pretrage <b> {{$q}} </b>: </p>
This is because I didn't include a $q variable. Just append it to your response in case you need it:
// ...
return view('search')->withDetails($posts)->withTotal($sum)->with('q', request('q'));
// ...
Related
So I'm making a fliter in laravel for a project and me and my teacher are braking our head on a the following thing.
In the code below the general search for a player works but the other statements won't add to it if they are defined and in the POST request;
Controller:
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class FilterController extends Controller
{
public function filter(Request $request)
{
$player = new \App\Player;
$filters = $player->newQuery();
$query = Input::get('q');
// Search for a player based on their status.
if ($request->has('status')) {
$filters->orwhere('status', $request->input('status'));
}
// Search for a player player on their club.
if ($request->has('club')) {
$filters->orwhere('Club', $request->input('club'));
}
// Search for a player player on their team category .
if ($request->has('Category')) {
$filters->orwhere('Category', $request->input('Category'));
}
// Search for a player player if he is flagged as removed.
if ($request->has('remove')) {
$filters->orwhere('remove', $request->input('remove'));
}
// Search for a player player on their size.
if ($request->has('size')) {
$filters->orwhere('Size', $request->input('size'));
}
// General search for a player
if($request->has('q')){
$filters->orwhere('first_name','LIKE','%'.$query.'%')
->orWhere('last_name','LIKE','%'.$query.'%')
->orWhere('mobile','LIKE','%'.$query.'%')
->orWhere('street_name_nummer','LIKE','%'.$query.'%')
->orWhere('city','LIKE','%'.$query.'%');
}
// Get the results and return them.
$results = $filters->get();
if(count($results) > 0){
return view('lists/ekick')->withDetails($results,$query);
} else return view ('lists/ekick')->with('No Details found. Try to search again !');
}
}
route:
Route::any('lists/ekick', 'FilterController#filter');
output view:
img from view
To pick up on Aaron Sarays answer, you are most likely filtering the wrong way. Normally additional filters are additional conditions each record has to meet in order to be part of the result. If you consider an Excel table and you filter one column, you won't even have all options to filter for in the second column because you already limited the result and you can only limit it further.
Combine this knowledge with an improved way of filtering and you receive a query like this:
public function filter(Request $request)
{
$query = $request->input('q');
$results = \App\Player::query()
->when($request->input('status'), function ($query, $status) {
$query->where('status', $status);
})
->when($request->input('club'), function ($query, $club) {
$query->where('club', $club);
})
->when($request->input('category'), function ($query, $category) {
$query->where('category', $category);
})
->when($request->input('remove'), function ($query, $remove) {
$query->where('remove', $remove);
})
->when($request->input('size'), function ($query, $size) {
$query->where('size', $size);
})
->when($query, function ($query, $q) {
$query->where(function ($query) use ($q) {
$query->where('first_name', 'LIKE', "%$q%")
->orWhere('last_name', 'LIKE', "%$q%")
->orWhere('mobile', 'LIKE', "%$q%")
->orWhere('street_name_number', 'LIKE', "%$q%")
->orWhere('city', 'LIKE', "%$q%");
});
})
->get();
if ($results->isNotEmpty()) {
return view('lists/ekick')->withDetails($results, $query);
} else {
return view ('lists/ekick')->with('No Details found. Try to search again !');
}
}
The function when($condition, $callback) as used in the query above is used to dynamically build queries. You can consider the following two statements equivalent:
// option 1: conditional query (preferred!)
$results = Player::query()
->when($request->input('q'), function ($query, $q) {
$query->where('name', 'LIKE', "%$q%");
})
->get();
// option 2: plain php query building... (not very clean code)
$query = Player::query();
if ($request->input('q')) {
$query->where('name', 'LIKE', '%'.$request->input('q').'%');
}
$results = $query->get();
In order to do what you're doing, I think you want to not use or with your queries. You're basically saying
Give me the Player where status is something or size is something
I think what you mean to say is
Give me the Player where status is something and size is something
Depending on if the requirements exist or not in the filter.
So, you'd want to alter your code using the following as an example:
if ($request->has('status')) {
$filters->where('status', $request->input('status'));
}
// Search for a player player on their club.
if ($request->has('club')) {
$filters->where('Club', $request->input('club'));
}
You can also bypass one step by using this:
$query = \App\Player::getQuery();
I'm trying to write a method in my controller to allow searching the results of my query on my view page. The query selects all results from the "ads" table and this method should allow filtering results by the ad's name inputting keywords in the search bar.
The controller code goes like this:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::paginate(10);
if (!empty($request->input('search_all'))) {
$search_all = urldecode($request->input('search_all'));
$ads->where(function ($query) use ($search_all) {
$query->where('name', 'like', '%'.$search_all.'%')->get();
});
}else {
// Returning to view
return view('admin.ad.list')
->with('ads', $ads)
->with('title', $title);
}
}
However, when I run a search I get the following error: "Missing argument 2 for Illuminate\Support\Collection::where()".
What am I doing wrong?
At the top, when doing $ads = Ad::paginate(10); you have already pulled the results from the database so no more filtering can be done at DB level.
I'd do something like this instead:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::select();
if ($request->input('search_all')) {
$search_all = urldecode($request->input('search_all'));
$ads->where('name', 'like', '%'.$search_all.'%');
}
// Returning to view
return view('admin.ad.list')
->with('ads', $ads->paginate(10))
->with('title', $title);
}
I'm new at laravel. I'm working on new web application using laravel 5.4. I want to count the number of data in a database and show the result in view. So, I have used this code in controller:
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
return view('/dashboard')->with(['counts'=> $counts]);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
return view('/dashboard')->with(['count'=> $count]);
}
and in view:
#if(count($counts)== 0)
no call record to be viewed
#else
<div class="huge">{{$counts}}</div>
#endif
#if(count($count)==0)
no call record to be viewed
#else
<div class="huge">{{$count}}</div>
#endif
but there is an error: undefined variable.
Please resolve this error.
public function bendingImgCount() {
return requestImg::where('is_done', '0')->count();
}
public function uploadedImgCount() {
return = requestImg::where('is_done', '1')->count();
}
public function showDashboard() {
$bendingImgCount = $this->bendingImgCount();
$uploadedImgCount = $this->uploadedImgCount();
return view('/dashboard')->with(compact(['bendingImgCount', 'uploadedImgCount']));
}
Call the showDashboard function and change in the view the counts to bendingImgCount and count to uploadedImgCount.
First you check wherether you get correct data at controller
You just simply
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
dd($counts);
return view('/dashboard')->with(['counts'=> $counts]);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
dd($count);
return view('/dashboard')->with(['count'=> $count]);
}
And check you get correct answer or not .
I am getting Correct count in browser
1.Controller Page:
2.web.php Page:
3.Web Browser:
If you are even not getting correct value then just show your whole code of page.
Thank you.
TRY THIS:
In Controller:
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
return view('/dashboard')->with('this_is_it', $counts);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
return view('/dashboard')->with('count_1',$count);
}
In View:
#if(count($this_is_it)== 0)
no call record to be viewed
#else
<div class="huge">{{$this_is_it}}</div>
#endif
#if(count($count_1)==0)
no call record to be viewed
#else
<div class="huge">{{$count_1}}</div>
#endif
I Have a search box that I want to use to search some of columns of a table in the database. Here's the code
$project = Project::findOrFail($id);
$file = \App\File::find($file);
$query = $request->input('q');
$materials = $query
?\App\Material::where('file_id', '=', $file->id)
->where('material_name', 'LIKE', "%$query%" )->get()
:\App\Material::where('file_id', '=', $file->id)->get();
return view('projects.file',compact('project', 'file', 'materials'));
The data as it is when the page loads is filtered to show just the items from this project. But when the search is done, it searches the whole table. How can I make it search only the items from the specific project and not the whole items from the table?
You can nest your search items this way:
public function handle($request, Closure $next)
{
$project = Project::findOrFail($id);
$file = \App\File::find($file);
$materials = \App\Material::newQuery();
// Did it found the file?
if ($file) {
// Search for the file
$materials->where('file_id', '=', $file->id);
}
// AND, does it have a query to search?
if ($search_item = $request->input('q')) {
// Search for the query
$materials->where('material_name', 'LIKE', "%$search_item%");
}
// AND, does it have a project to filter?
if ($search_item = $request->input('project')) {
// Filter the project
$materials->where('project', 'LIKE', "%$search_item%");
}
// Now go get the results
$materials = $materials->get();
// And return to the view
return view('projects.file',compact('project', 'file', 'materials'));
}
I am trying to create a page where I can see all the people in my database and create edits on them. I made a form where I fill in the data from the database of certain fields.
I would like to navigate trough them by a Next and Previous button.
For generating the next step I have to take the ID larger than the current one to load the next profile.
For generating the previous step I have to take the ID smaller than the current one to load the previous profile.
My route:
Route::get('users/{id}','UserController#show');
Controller:
public function show($id)
{
$input = User::find($id);
// If a user clicks next this one should be executed.
$input = User::where('id', '>', $id)->firstOrFail();
echo '<pre>';
dd($input);
echo '</pre>';
return View::make('hello')->with('input', $input);
}
View:
The buttons:
Next
What is the best approach to get the current ID and increment it?
Below are your updated controller and view files derived from #ridecar2 link,
Controller:
public function show($id)
{
// get the current user
$user = User::find($id);
// get previous user id
$previous = User::where('id', '<', $user->id)->max('id');
// get next user id
$next = User::where('id', '>', $user->id)->min('id');
return View::make('users.show')->with('previous', $previous)->with('next', $next);
}
View:
Previous
Next
// in your model file
public function next(){
// get next user
return User::where('id', '>', $this->id)->orderBy('id','asc')->first();
}
public function previous(){
// get previous user
return User::where('id', '<', $this->id)->orderBy('id','desc')->first();
}
// in your controller file
$user = User::find(5);
// a clean object that can be used anywhere
$user->next();
$user->previous();
In your App\Models\User.php
...
protected $appends = ['next', 'previous'];
public function getNextAttribute()
{
return $this->where('id', '>', $this->id)->orderBy('id','asc')->first();
}
public function getPreviousAttribute()
{
return $this->where('id', '<', $this->id)->orderBy('id','asc')->first();
}
In your Controller you can simply do this:
public function show(User $user)
{
return View::make('users.show')
->with('user', $user)
->with('previous', $user->previous)
->with('next', $user->next);
}
I understand the approach being taken here by user2581096 but I am not sure it is efficient (by any standards). We are calling the database 3 times for really no good reason. I suggest an alternative that will be way more efficient and scalable.
Do not pass the previous and next IDs to the view. This eliminates 2 unnecessary database calls.
Create the following routes:
users/{id}/next
users/{id}/previous
These routes should be used in the href attributes of the anchor tags
Add methods in the controller to handle each of the new routes you have created. For example:
public function getPrevious(){
// get previous user
$user = User::where('id', '<', $this->id)->orderBy('id','desc')->first();
return $this->show($user->id);
}
This function will only be called when you actually click on the button. Therefore, the database call is only made when you need to actually look up the user.
in-case you want to retrieve the prev/next records along with their data,
you can try
$id = 7; // for example
$prev = DB::table('posts')->where('id', '<', $id)->orderBy('id','desc')->limit(1);
$next = DB::table('posts')->where('id', '>', $id)->limit(1);
$res = DB::table('posts')
->where('id', '=', $id)
->unionAll($prev)
->unionAll($next)
->get();
// now $res is an array of 3 objects
// main, prev, next
dd($res);
1- the query builder is usually much faster than eloquent.
2- with union we are now only hitting the db once instead of 3.
To get next and previous post we can use max and min functions on Model id in laravel. here is an example to get this
https://usingphp.com/post/get-next-and-previous-post-link-in-laravel
The Controller:
public function post($id)
{
$post = Post::find($id);
$previous = Post::where('id', '<', $post->id)->max('id');
$next = Post::where('id', '>', $post->id)->min('id');
return view( 'post', compact( 'post', 'next', 'previous' ));
}
The View:
#if($next)
{{$next->title}}
#endif
#if($previous)
{{$previous->title}}
#endif
Here's a link I found that should help: http://maxoffsky.com/code-blog/laravel-quick-tip-get-previous-next-records/
It looks like for next you want to use: $next = User::where('id', '>', $id)->min('id'); and have the view as: Next
Also don't forget to pass $next to the view.
Simplest approach
// User.php
public static function findNext($id)
{
return static::where('id', '>', $id)->first();
}
// UserController.php
$nextUser = User::findNext($id);
// view
Next
Lazy approach :
// view
Next
// routes.php (should be optimized, this is just to show the idea)
Route::get('users/{user}/next', function($id) {
$nextUser = User::findNext($id);
return Redirect::to('user/' . $id);
});
// yourModel.php
public function previous()
{
return $this->find(--$this->id);
}
public function next()
{
return $this->find(++$this->id);
}
Works like magic, you can chain it:
$prevprev = Model::find($id)->previous()->previous();
$nextnext = Model::find($id)->next()->next();
First, get a record out of the database.
$post = Post::where('slug', $slug)->first();
With a database record, we can get the previous record where the record id is less than the id stored inside $post order by the id in descending order and use first() to get a single record back.
$previous = Post::where('id', '<', $post->id)->orderBy('id','desc')->first();
To get the next record it's almost the same query, this time get the record where the id is more than the id stored in $post.
$next = Post::where('id', '>', $post->id)->orderBy('id')->first();
Controller:
public function show($id)
{
// get the current user
$user = User::find($id);
// get previous user id
$previous = User::offset($user->id-2)->first();
// get next user id
$next = User::offset($user->id)->first();
return View::make('users.show')->with('previous', $previous)->with('next', $next);
}
i developed the code.
it work all times, even if we don't have any next or prev post
public function nextPost($table, $id)
{
$next = DB::table($table)->where('id', '>', $id)->orderBy('id','asc')->first();
if(!$next)
$next = DB::table($table)->orderBy('id','asc')->first();
return $next;
}
public function prevPost($table, $id)
{
$prev = DB::table($table)->where('id', '<', $id)->orderBy('id','desc')->first();
if(!$prev)
$prev = DB::table($table)->orderBy('id','desc')->first();
return $prev;
}