Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

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);
	}

Last updated 3 years ago.
0

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 :) :)

Last updated 10 years ago.
0

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..

0

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

Last updated 10 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

George george Joined 10 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.