app/Http/Controllers/HomeController.php
/**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
Show the application dashboard.
@return \Illuminate\Contracts\Support\Renderable
Show the application dashboard . @return \ Illuminate \ Contracts \ Support \ Renderable
public function index()
    {
        $categories = Category::all();
        $products = Product::all();
        $carts = Order::where('status_id', '1')->where('user_id', Auth::user()->id)->get();
        foreach($products as $product){
           $total_star = 0;
           $count = 0;
           $avg_star = 0;
           foreach($product->stars as $star){
                $total_star += $star->value;
                $count++;
           }
           if($count > 0){
               $avg_star = $total_star / $count;
           }
           $product->star = $avg_star;
        }   
        return view('home', [
            'categories' => $categories,
            'products' => $products,
            'carts' => $carts,
        ]);
    }
public function index ( ) { $ categories = Category :: all ( ) ; $ products = Product :: all ( ) ; $ carts = Order :: where ( 'status_id' , '1' ) -> where ( 'user_id' , Auth :: user ( ) -> id ) -> get ( ) ; foreach ( $ products as $ product ) { $ total_star = 0 ; $ count = 0 ; $ avg_star = 0 ; foreach ( $ product -> stars as $ star ) { $ total_star += $ star -> value ; $ count ++ ; } if ( $ count > 0 ) { $ avg_star = $ total_star / $ count ; } $ product -> star = $ avg_star ; } return view ( 'home' , [ 'categories' => $ categories , 'products' => $ products , 'carts' => $ carts , ] ) ; }
Show the application dashboard.
Show the application dashboard .