displaying the brands in filter panel for a ecommerce site

Published 6 months ago by Shawdow

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
       
wilk_randall

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

Shawdow

@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

@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',
     ];

    
}
wilk_randall

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.

wilk_randall

For more information on relationships, please refer to the following link: https://laravel.com/docs/5.5/eloquent-relationships

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

wilk_randall

@lostdreamer_nl - You do realize I already said all of that right??

Shawdow

@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

wilk_randall

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

Shawdow

@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

wilk_randall

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

Shawdow

@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

@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

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

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

Please sign in or create an account to participate in this conversation.