dimanche 6 septembre 2020

Laravel aggregate sum by id not showing on Blade

I have Developed an inventory management system where products table and order_details table are available. Now I want to show how many products are sold according to product_id and the summing amount to show on the blade template loop. Where Product table ID equal to order_details product_id. But unfortunately, it's not showing.

The Product Controller index Function

public function index()
{
    $orderdetail = DB::table('order_details')
            ->select('quantity', DB::raw('sum(quantity) as sum'))
            ->groupBy('product_id')
            ->havingRaw('SUM(quantity)')
            ->get();

    $products = Product::latest()->with('category', 'supplier', 'orderdetail')->get();
    return view('admin.product.index', compact('products', 'orderdetail'));

}

The Product Model

class Product extends Model
{

protected $dates = [
    'buying_date', 'expire_date',
];

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

public function supplier()
{
    return $this->belongsTo(Supplier::class);
}

public function orderdetail()
{
    return $this->belongsTo('App\OrderDetail', 'id', 'product_id');
}

}

and the Blade

@foreach($products as $key => $product)
<tr>
    <td></td>
    <td></td>
    <td>
        <img class="img-rounded" style="height:35px; width: 35px;" src="" alt="">
    </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td>000000</td>
    <td></td>
    <td>
        <div class="btn-group">
            <button type="button" class="btn btn-default">Click</button>
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
            </button>
            <ul class="dropdown-menu" role="menu" style="min-width:auto">
            <li><a href="">Show</a></li>
            <li><a href="">Edit</a></li>  
            </ul>
        </div>
        
    </td>
</tr>

@endforeach

But Its showing nothing. After placing

dd($orderdetail, $products);

It's Showing this result. The Results of Sum

So anybody there who can help to solve this prob. I can't able to show on the blade.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire