ShawT's avatar
Level 1

How to display single category in breadscrum

I'm trying to display product Category and subcategory in breadscrum. It's showing me all my categories like Pants > Gloves > T-shirt etc.. I want just one.. I'm trying to get it from a controller. How to code $categories to get just one?

My controller: https://pastebin.com/yGaLZBHc

                        <li class="breadcrumb-item br-item" style="margin-left: -1%;">
                            <a href="https://blackstarwear.md">Главная</a>
                        </li>
                        <li class="breadcrumb-item br-item" style="margin-left: -1%;">
                            <a href="#">Мужская</a>
                        </li>
                        @foreach($categories as $category)
                        <li class="breadcrumb-item br-item" style="margin-left: -1%;">
                            
                            <a data-link="{{$category->id}}" href="{{ url('catalog/main/' . $category->id) }}"> {{ $category->name }}</a>
                        </li>
                        @endforeach
                        <li class="breadcrumb-item br-item" aria-current="page">
                            <a href="{{ url('/product/'.$product->id) }}">{{ $product->name }}  {{ $product->second_name }}</a>
                        </li>
                        
                    </ol>
                </nav>
            </div>
        </div>
    </section>
    
0 likes
11 replies
ShawT's avatar
Level 1
@foreach($categories as $category)
<li class="breadcrumb-item br-item" style="margin-left: -1%;">
                        
                        <a data-link="{{$subcategory->category->id}}" href="{{ url('catalog/main/' . $subcategory->category->id) }}"> {{ $subcategory->category->name }}</a>
                    </li>
@endforeach
Undefined variable: subcategory (View: /var/www/u1066825/public_html/site.com/resources/views/layouts/main/product-page.blade.php)
Previous exceptions
Undefined variable: subcategory (0)
SilenceBringer's avatar

Hi @shawt not sure I understand correctly what you want... If you ned just 1 category, why you use loop?

insted of

@foreach($categories as $category)
    ...
@endforeach

output the first one

<li class="breadcrumb-item br-item" style="margin-left: -1%;">
    <a data-link="{{$categories->first()->id}}" href="{{ url('catalog/main/' . $categories->first()->id) }}"> {{ $categories->first()->name }}</a>
</li>

or in your controller replace

$categories = Categories::where('subcategory', 0)->get();

with

$category = Categories::where('subcategory', 0)->first();

it will give you 1 category instead of collection

Hope it helps. Or try to explain better what you need

ShawT's avatar
Level 1

It works, but it show first category from database :) I want to show current category and subcategory on product page

ShawT's avatar
Level 1

product has associated one category and one subcategory

SilenceBringer's avatar

you can use Relationships to get category of the given product

if your product model:


public function category () {
    return $this->belongsTo(Category::class);
}

then you can just call

dd($product->category);

to get category associated with the given product.

ShawT's avatar
Level 1

No, my product model is just

namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model { protected $guarded = ['id']; }

Do i have to add function with return hasMany to category model also or not?

SilenceBringer's avatar

@shawt modify your product class

namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model {
    protected $guarded = ['id'];

    public function category () {
        return $this->belongsTo(Category::class);
    }
}

then in your controller call

dd($product->category);

it will returns category associated with the given product

ShawT's avatar
Level 1

I tried and it prints $product->category :)

SilenceBringer's avatar

@shawt can you show the code when you do it? or try this (do not forget to include category method in your Product model)

class Catalog extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($id)
    {
        $products       = Products::with('category')->where('category_id', $id)->where('out_of_stock', 0)->orderBy('created_at', 'desc')->paginate(9);

        // this will give you associated category for the first product on the page
        dd($products->first()->category);

        // or inside the loop
        foreach ($products as $product) {
            dd($product->category);
        }


        // other stuff here
    }

    // other methods here
}

Please or to participate in this conversation.