Home > Software engineering >  Laravel Query Builder create looping data from category
Laravel Query Builder create looping data from category

Time:11-23

I am quite new in Laravel. I've googled with this keyword but no luck. I use Laravel 8. Currently I need to view data from each category, so it will be like this on blade:

cat A
  product A1 <img src=(get from thumbnail)>
  product A2 <img src=(get from thumbnail)>

cat B
  product B1 <img src=(get from thumbnail)>
  product B2 <img src=(get from thumbnail)>

etc...

Currently my controller is:

    $categories = DB::table('tbl_categories')
        ->orderBy('name')
        ->get();
        
        foreach($categories as $key) {
          $data = DB::table('tbl_product')
          ->where('status','Enable')
          ->where('category_id',$key->id)
          ->get();
        } 

        $thumbnail = DB::table('tbl_thumbnails')
        ->where('product_id',$data[0]->id)
        ->get();

return view('/products', ['categories' => $categories, 'data' => $data, 'thumbnail' => $thumbnail]);

in my blade:

@foreach($categories as $discover_category)
<div>
  @foreach($data as $discover)
  @foreach($thumbnail as $a)
  <!-- product name and thumbnail in here-->
  @endif
  @endif
</div>
@endif

But the result is now show only last category_id. please help. GBU.

CodePudding user response:

you need to use first() to get single value like this

foreach($categories as $category) {

    $products = DB::table('tbl_product')
        ->where('status','Enable')
        ->where('category_id',$category->id)
        ->get();
    
    foreach ($products as $product) {
        $thumbnail = DB::table('tbl_thumbnails')
            ->where('product_id',$product->id)
            ->first();
        // assign value to $product
        $product->thumbnail = $thumbnail;
    }
    
    // assign value to $category
    $category->products = $products;
}

then in view you can use like

@foreach($categories as $category)
<div>
@foreach($category->products as $product)
   
        <div>
            <p>{{$product->name}}</p>
            <p>{{$product->thumbnail->id}}</p>
        </div>
  <!-- product name and thumbnail in here-->

  @endif
</div>
@endif

CodePudding user response:

You can use Eloquent ORM, Just simply use this kind of code on your category model like:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    /**
     * Get the products for the category.
     */
    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

On controller like

$categories = Category::all();
return view('products', ['categories' => $categories]);

On Blade like

@foreach($categories as $category)
<div>
  <div>{{ $category->name }}</div>
  @foreach($category->products as $products) // here this products object called form model
    @foreach($products as $product)
    <!-- product name and thumbnail in here-->
    @endforreach
  @endforreach
</div>
@endforeach

Note

Note: You must perfectly make the relationship between the products table and categories table. Products table must have a column named category_id

  • Related