Shawdow's avatar

displaying the brands in filter panel for a ecommerce site

Hi,

i need to display the brand for a particular products i explored in the some ecomerce site filter panel was changing as the products gets displayed

i have brand table now i need know the how the brands get displayed for every product

in the brands table

id        name           category_id 
1           hp                      1
2         sony                    1


in the categories table

id           name                       parent_id 
1             electronics                 null 
2           house appliances      null
3            laptops                            1

logic i have written if there is any wrong please correct me

in the controller

public function filterCategory() {

$category = DB::table('categories')->select('id')->get();    
 

$brand = DB::table('brands')->select('*')->get();

return view('category',compact('category','brand));

}


in the blade file

@foreach($category as $categories)

@foreach($brand as $brands)

if($categories->id as $brands->category_id)

Brand:{{ $brands->name }}

@endif
@endforeach
@endforeach
       
0 likes
18 replies
rawilk's avatar

Are you not using relations? This would be a lot easier with them. Also, your naming conventions are a little weird, as you're using singular name for a collection and a plural name for a single instance.


public function filterCategory() {
    $categories = Category::with('brands')->select('id')->get();

    return view('category', compact('categories'));
}


@foreach ($categories as $category)
    @foreach ($category->brands as $brand)
        Brand: {{ $brand->name }}
    @endforeach
@endforeach

1 like
Shawdow's avatar

@wilk_randall sorry sir it was typo mistake. i have discussed the other thread some laravelers told me use the relationship. i have used the relationship but there no use according to my logic

Shawdow's avatar

@wilk_randall can you help me with relationship ??

brand model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Brand extends Model
{
     public $fillable = ['name'];

    protected $casts = [
        'category_id' => 'int',
     ];

  
}

category model


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
     public $fillable = ['name'];

    protected $casts = [
        'parent_id' => 'int',
     ];

    
}
rawilk's avatar

You should be able to modify your category model like this to work:


class Category extends Model
{
    protected $fillable = ['name'];

    protected $casts = [
        'parent_id' => 'integer'
    ];

    public function brands() {
        return $this->hasMany(Brand::class);
    }
}

And just as an FYI, $fillable is a protected property, not public.

1 like
lostdreamer_nl's avatar
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Brand extends Model
{
    public $fillable = ['name'];

    protected $casts = [
        'category_id' => 'int',
    ];
    
    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public $fillable = ['name'];

    protected $casts = [
        'parent_id' => 'int',
    ];
    
    public function brands()
    {
        return $this->hasMany(Brand::class);
    }
}

Controller:

public function filterCategory() 
{
    $categories = Category::with('brands')->get();
    return view('category',compact('categories'));
}

View:

@foreach ($categories as $category)
    <h2>{{ $category->name}} </h2>
    <ul>
    @foreach ($category->brands as $brand)
        <li>{{ $brand->name }}</li>
    @endforeach
    </ul>
@endforeach

That should do....

Shawdow's avatar

@wilk_randall relationship logic works with tinker!!

but it displays all the brands. i need that displaying the brands for a particular product if it is present so i wrote if else in blade file for comparing the category_id in the brands table and id in the categories table

rawilk's avatar

How are you displaying it? The relationship shouldn't pull all the brands, only those with that category id.

1 like
Shawdow's avatar

@wilk_randall yeah but its not displaying!!!

public function filterCategory(){

 $categories = \App\Category::with('brands')->get();

 return view('category',compact('categories'));

}

blade

@foreach ($categories as $category)
    <h2>{{ $category->name}} </h2>
    <ul>
    @foreach ($category->brands as $brand)
        <li>{{ $brand->name }}</li>
    @endforeach
    </ul>
@endforeach

rawilk's avatar

What is not displaying? The brands? Are you sure they have the correct category_id attached to them?

1 like
Shawdow's avatar

@wilk_randall brands are displaying for all the subcategories not based on id match.yeah i have defined category_id in the brands table which is references to the categorie's id column

lostdreamer_nl's avatar

@wilk_randall and you do realize that typing a comment takes time, and in the time that I typed mine, you posted yours? (When i was typing, the last comment was "can you help me with relationship")

Not to sound like a ..... just to bounce the ball back ;)

Shawdow's avatar

little clarification for the above logic does it shows all the brands when the user clicks on the laptops(it shows me all the brands)

for example if the users clicks on the laptops subcategory whether it shows brands

//Brands(all the brands) 

hp
sony
samsung
godrej


//or Brands(which matches the category_id)

samsung
godrej

//in the brands table

id        name           category_id 
1           hp                       1
2         sony                     1
3         samsung            2
4         godrej                 2

//in the categories table

id           name                           parent_id 
1             electronics                     null 
2           house appliances         null
3            laptops                             1
4           refrigirator                      2

lostdreamer_nl's avatar

with the above logic it would only show the brands that match the category_id ;) To get all brands, just use $brands = Brands::all();

Shawdow's avatar

@lostdreamer_nl I need brands that match the category_id only sir but it displays for all the categories not for the particular categories

suppose if i have smart phone as category all the brands gets displayed in the smart phone category not the brands which relates to the smart phone!!

Shawdow's avatar

@lostdreamer_nl example

1.smart phone,tablets,refrigirator(subcategories)

  1. for both smart phone and tablets i have brands like (samsung,sony)

3.but the logic written shows brands (samsung,sony) for refrigirator also it should not show right??

Shawdow's avatar

@lostdreamer_nl i have tried with below logic also to show only for particualr subcategory(naming variable not good)

@foreach ($brand as $category)

    @foreach ($category->brands as $brand)

@if($category->id == $brand->category_id)

        {{ $brand->name }} <input type="checkbox"><br>
 
@endif
    @endforeach

@endforeach

Please or to participate in this conversation.