ORM Kohana Pagination VS total records found - php

I'm having some problem with this script... works fine, but not for the total of records found on the query.
I have this function on my ORM called "mix". There are 3 tables: mixes, generos and mixes_generos.
I can search in the table "mixes" or on the table "generos", anyone.
public function search($query, $itemsperpage, $page)
{
if (empty($page))
$page = 1;
$offset = ($page - 1) * $itemsperpage;
$orm = $this->join('mixes_generos', 'LEFT')->on('mix.id', '=', 'mixes_generos.mix_id')
->join('generos', 'LEFT')->on('generos.id', '=', 'mixes_generos.genero_id')
->where_open()
->or_where('mix.nombre', 'LIKE', '%'.$query.'%')
->or_where('mix.descripcion', 'LIKE', '%'.$query.'%')
->or_where('generos.nombre', 'LIKE', '%'.$query.'%')
->where_close()
->and_where('mix.estatus', '=', 'active')
->order_by('mix.fecha_produccion', 'DESC')
->order_by('mix.track', 'DESC');
$this->_search_total = $orm->reset(FALSE)->count_all();
$orm = $orm->limit($itemsperpage)->offset($offset);
return $orm;
}
public function search_total()
{
return $this->_search_total;
}
Then, in my controller i have this:
$data = ORM::factory('mix')->search('pop music', 10, 1);
if I want to echo the total, i do this:
echo $data->search_total();
in my view I print the info:
foreach($data->find_all() as $mix)
echo $mix->nombre;
http://s10.postimage.org/cvnr28nop/records.jpg
in this link you will see the result, look closely the column "records_founds" vs the number of "records" that it founds, 3 vs 1??.
And that is the main problem... because when i print the data of the mixes shows 3 instead 1. I also add an group_by('mix.id') and nothing :(
What do you think?
Thank you

Related

Check if auth user has voted and then take count of polls?

I encountered an issue that blocks my way forward.
I have tried getting count of polls as of now as shown below:
public function user_dashboard(){
$activepolls = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->count();
return view('user_dashboard',compact('activepolls'));
}
As of Now I am thinking that this below function would have help me but i am not sure.
public function hasVoted($poll_id)
{
$poll = Poll::findOrFail($poll_id);
if ($poll->canGuestVote()) {
$result = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->where('larapoll_options.poll_id', $poll_id)->count();
return $result !== 0;
}
return $this->options()->where('poll_id', $poll->id)->count() !== 0;
}
Simply i had made a poll box in my website and i want to show the count of polls in right corner of that box.
i need the count of poll in which user has not voted.
if user votes on poll it should remove from count as well.
If anyone can help , the response would much appreciated..
Thanks..
I have Solved The issue by following code...
$activepolls = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->count();
$polls = Poll::count();
$unvotedpolls = ($polls - $activepolls);
in ($unvotedpolls) i got the count of polls in which auth user has not voted...

Laravel how to write search query with multiple conditions but where records under the same user

I have a database with inventory products. Also, I have many users where products belonged to different users.
So I need to write a query with multiple conditions, where search needs to be in different table columns, so it will be a couple of ->orWhere() statements. But the search needs to find all the products that belonged to the same user. But if I have many ->orWhere() conditions, query starts searching the products under other users where other queries conditions match ("title" or "product_number").
So how can I change my code, where I can get all products with search keyword where the products under the same user?
Here is my code:
public function searchProduct(Request $search){
$search_item = $search->search;
$user = User::where('id', Auth::user()->id)->first();
$inventory = Inventory::where('client_id', $user->client_id)
->where('product_number', 'like', "%{$search_item}%")
->orWhere('title', 'like', "%{$search_item}%")
->orWhere('other_product_numbers', 'like', "%{$search_item}%")
->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
->paginate(50);
$cleint_currency = SettingsClientCurrency::where('client_id', $user->client_id)->first();
$inventory_products = Inventory::where('client_id', $user->client_id)->with('orderInRecord')->get();
$inventory_total = 0;
foreach ($inventory_products as $product) {
$ordersin = $product->orderInRecord;
foreach ($ordersin as $orderin) {
$inventory_total += $orderin['price'] * $orderin['units'];
}
}
return view('layouts.inventory.inventory')
->with('inventory', $inventory)
->with('inventory_total', $inventory_total)
->with('cleint_currency', $cleint_currency)
->with('user', $user);
}
you can use two where clause with a callback function in the second where clause. that will get your desired result
$inventory = Inventory::where('client_id', $user->client_id)
->where(function ($query) use ($search_item) {
$query->where('product_number', 'like', "%{$search_item}%")
->orWhere('title', 'like', "%{$search_item}%")
->orWhere('other_product_numbers', 'like', "%{$search_item}%");
})
->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
->paginate(50);

How to Total Quantity using the Controller and return to view

Have a total sum of the dr_quantity in the controller and return to the view model
public function searchmedreport()
{
$search = \Request::get('search');
$total = DB::table('distribution_records')->where('id', Auth::user()->id)
->sum('medicine_name', 'LIKE', '%'.$search.'%');
$records = DistributionRecord::whereRaw("Concat(dr_fname,' ',dr_lname) LIKE '%{$search}%' ")
->orWhere('medicine_name','LIKE','%'.$search.'%')
->orWhere('date_requested','LIKE','%'.$search.'%')
->orderby('id')->paginate(5000);
return view('forms.searchmedreport',['records'=>$records,'total'=>$total]);
}
and
<b><h4>Total Number of Medicine Distributed: {{$total}} </b></h4>
put the total in
Based on your table, you are performing a wrong query, so instead of this:
$total = DB::table('distribution_records')->where('id', Auth::user()->id)
->sum('medicine_name', 'LIKE', '%'.$search.'%');
You need this:
$total = DB::table('distribution_records')->where('user_id', Auth::user()->id)
->where('medicine_name', 'LIKE', '%'.$search.'%')->sum('dr_quantity');

Need help getting the right query on repository laravel 4.2

I need to fetch the categorie name from the images to the ImagesRepository
So far in ImagesRepository i got:
public function latest($limit = 8)
{
return $this->image->whereNotNull('poster')
->orderBy('id', 'desc')
->limit($limit)
->get();
}
I tried using leftJoin but it didnt work:
public function latest($limit = 8)
{
return $this->image->whereNotNull('poster')
->orderBy('id', 'desc')
->limit($limit)
->leftJoin('category', 'id', '=', 'category_id')
->get();
}
Now i need to get the id from the Category Table that matches the category_id so i can get the right url link to the post.
Cause my result right now is :
localhost/test/1/name-1
and i need to get:
localhost/test/1-flowers/name-1
OK so after carefully looking at laravel documentation i figured it out the solution since they are already relationships in the eloquent all i have to do is call the table by simple adding a line...
public function latest($limit = 8)
{
return $this->image->whereNotNull('poster')
->with('category')
->orderBy('id', 'desc')
->limit($limit)
->get();
}

Laravel, how to call or append(join) another model with condition in existing model condition

I am stuck with search functionality from two different tables (VenueHall and Venue). The data is coming in the search1 parameter. I am able to search the data from one table (VenueHall) but I am not able to map with another table (Venue) using the existing object from the first table ($VenueHall`).
$VenueHall = VenueHall::orderBy('created_at', 'DESC');
$VenueHall = $VenueHall->with('venue');
$VenueHall = $VenueHall->with('venueType');
$VenueHall = $VenueHall->with('venue.area');
if (Input::has('query') && $searchQuery = Input::get('query'))
{
$perecentilify = '%'. $searchQuery .'%';
$VenueHall = $VenueHall->where(function($query) use ($perecentilify, $searchQuery) {
$query->where('name', 'LIKE', $perecentilify)
->orWhere('code', 'LIKE', $perecentilify);
//->orWhere('from venue table');
});
}
$data = array(
'venueHall' => $VenueHall->paginate(2)
);
return View::make('venues/venues', array('data' => $data));
I am not able to map the //->orWhere('from venue table') clause which comes from the Venue table. Please help me out get the data from the two tables based on the search value in the search box.
Often I find joins to be the easiest solution in such cases:
$VenueHall = VenueHall
::join('Venue', 'Venue.id', '=', 'VenueHall.venue_id')
->where('VenueHall.name', 'LIKE', $perecentilify)
->orWhere('VenueHall.code', 'LIKE', $perecentilify)
->orWhere('Venue.name', 'LIKE', $perecentilify)
->orderBy('created_at', 'DESC')
->with(['venue', 'venueType', 'venue.area'])
->paginate(2)
;

Categories