Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shiva's avatar
Level 5

Getting a search form to get category

I've created a search form that looks for the products being searched. I've managed to get that right, but I would like to link the searched products to its respective page. The problem I'm having is that I'm struggling to get the category so that I can create the link. The category is in another table.

For example the link will look like this:

http://localhost/product_site/Taps/tap-1

where Taps is the category.

here is my search.blade.php

@extends('templates::layouts.public')
@section('content')
    <h1>This is the search page</h1>

    @if(isset($details))
        <p>
            The search results for your query <b>{!! $query !!}</b> are:
        </p>

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>
                        Title
                    </th>
                </tr>
            </thead>

            <tbody>
                
                @foreach($details as $product)
                    <tr>
                        <td>
                            <a href="{!! route('product.item', [$product->title, $product->slug]) !!}">
                                {!! $product->title !!}
                            </a>
                        </td>
                    </tr>
                @endforeach
                
            </tbody>
        </table>
    @endif
@stop 

$product->title isn't correct. That is where I would like to get the category of the searched item.

My OpenController.php

<?php

namespace App\Modules\Open\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Input;
use Validator;
use Mail;
use App\Modules\Menus\Models\Menu;
use App\Modules\Products\Models\ProductsCat;
use App\Modules\Products\Models\Product;
use App\Modules\Contact\Models\Contact;

class OpenController extends Controller
{
    public function search()
    {
        $search = Input::get('search');

        $product = Product::where('title', 'LIKE', '%'.$search.'%')->get();

        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();

        $contact = Contact::all();

        $productsCat = ProductsCat::all();

        if(count($product) > 0)
        {
            return view('open::public.search', compact('menus_child', 'contact', 'productsCat', 'product'))->withDetails($product)->withQuery($search);
        }else{
            return view('open::public.search', compact('menus_child', 'contact', 'productsCat', 'product'))->withMessage('No products was found.');
        }
    }
}

My Product.php model

<?php

namespace App\Modules\Products\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    // Allows the fields in the array to be fillable
    protected $fillable = array('title', 'description', 'image', 'image2', 'slug');

    // Doesn't allow the id to be changed
    protected $guarded = array('id');

    protected $table = 'products';

    // Validation rules
    public static $rules = array(
                                'title' => '',
                                'description' => '',
                                'price' => '',
                                'image' => ''
                            );

    public function slug($title)
    {
        $this->attributes['title'] = $title;
        $this->attributes['slug'] = Str::slug($title);
    }

    public function productscat()
    {
        return $this->belongsToMany('App\Modules\Products\Models\ProductsCat', 'productscat_products', 'product_id', 'productcat_id');
    }
    
}

My ProductsCat.php model

<?php

namespace App\Modules\Products\Models;

use Illuminate\Database\Eloquent\Model;

class ProductsCat extends Model
{
     //Allows the fields in the array to be fillable
    protected $fillable = array('title', 'image', 'image2');
    //Doesn't allow the id to be changed
    protected $guarded = array('id');

    protected $table = 'productscat';

    //Validation rules
    public static $rules = array(
        
        );

    public function product()
    {
        return $this->belongsToMany('App\Modules\Products\Models\Product', 'productscat_products', 'productcat_id', 'product_id');
    }
}
0 likes
0 replies

Please or to participate in this conversation.