Hello,
I am having problem querying my database. If anyone can help it would be very appreciated.
I have the controller below which query the database from a form in the view and it works fine. Each of the results(phone model) has relationship with the Product as Phone -> has many -> Products
What i want to implemented is add a query to select ONLY the ones that has Products under them.
I tried several things like adding has('Product') in front of the first where without luck so want i think now is adding one more column as ProductCounter under Phones
Any help here?
Thanks in advance
<?php
class PhoneController extends BaseController{
public function getShow()
{
//$phones = Phone::paginate(24);
$phones = Phone::where(function($query){
$min_weight = Input::has('min_weight') ? Input::get('min_weight') : null;
$max_weight = Input::has('max_weight') ? Input::get('max_weight') : $max_weight = null;
$brands = Input::has('brands') ? Input::get('brands') : null;
if($min_weight==NULL){
$min_weight=0;
}
if($max_weight==NULL){
$max_weight=9999999999;
}
if(isset($brands)){
foreach ($brands as $brand) {
if(isset($min_weight) && isset($max_weight)){
$query-> orWhere('weight','>=',$min_weight);
$query-> where('weight','<=', $max_weight);
}
$query->where('manufacturer','LIKE', $brand);
}
}
if(isset($min_weight) && isset($max_weight) ){
$query-> where('weight','>=',$min_weight);
$query-> where('weight','<=',$max_weight);
}
})->paginate(10);
return View::make('phones.all', compact(['specs']))->with('phones',$phones);
}
Thanks so much for trying to help me DrPrez :) Unfortunately i am getting "Class 'App\Phone' not found" for some reason :/
DrPrez said:
This works great, try paste it in your routes-file and go to /test
EDIT: i removed "\App" and now it shows me the content of the variable correctly. Now i have to integrate it in my function
EDIT 2: Thanks DrPrez :) I now realized that 'has' is looking for methods and not for models :S my bad, Thank you :) :)
For some reason when i added 'has' it became very very slow :/ Any thoughts on that? If i remove it, the website is fast again..
with has 8.0563471317291 without has 0.050530910491943 calculations made using php microtime on controller beginning and ending
my database Phone -> 3425 rows Products -> 7631 rows
Also, i currently run it on my computer using XAMPP.
NOTE: This is now solved here -> http://laravel.io/forum/03-14-2015-timing-issues-when-use-has-relationship
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community