I think I already tried using q->producttype->...
I will try with the () indicating. Method call next...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello all,
I'm a confessed noob and new to the world of web app development with Laravel.
I'm a little bit stuck and was wondering if I'm missing something really obvious or need to change the way I'm working entirely.
I'm creating a side bar that allows a user to search the database for stored entries (products), using a checkbox style form. When submitted, the get form submits values to the ContentController - it collects the data and displays it in the main space using a forEach.
This Product table has a 'hasone' relation to another table because the products can be of many different types (electronic, gifts, office equipment etc).
I've developed ways to create products including uploading images etc, this is also storing the references to ProductType as a TinyInt in MySQL.
At present, I've managed to collect Products from the main table and reference the relational table data within the view. I'm stuck trying to query the products by their ProductType!
public function Aside(Request $request) {
$q = Product::query();
if (Input::has('Apple'))
{
$q->orwhere('Brand','=',Input::get('Apple'));
}
if (Input::has('Dell'))
{
$q->orwhere('Brand','=',Input::get('Dell'));
}
if (Input::has('HP'))
{
$q->orwhere('Brand','=',Input::get('HP'));
}
$Products = $q->orderBy('created_at')->paginate(3);
return view('Aside', compact('Products'));
}
The ProductType has a 'belongsTo' relationship - to Product The Product has a 'hasOne' relationship - to ProductType
(Currently) The above code will provide all Products that have had their choice brand selected within the checkbox. Multiple can be selected and duplicates aren't returned.
How would I include within the same controller method an if/orwhere query through a Products->ProductType table.
This method is on a ContentsController. I have a Product table and ProductType table.
Really puzzled. I hope someone can point me in the right direction.
Best regards,
Will
Just a reply to say that I managed to fix the issue.
The problem was that I needed to use table 'joining' in my query (because SQL isn't very smart and doesn't have any knowledge of the class and objects).
$q = Product::query()->join('Product_Types', 'Product_Types.id', 'Products.id’);
The filtering can then access columns of the joined table using the regular syntax.
... I bashed my head against the desk for hours trying to get this right. Almost to the point where I was considering merging the tables together. I hope it helps someone else out.
Please or to participate in this conversation.