how fix this error "Undefined variable: products (0)"

hrt

This is my : IndexPageController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;

class IndexPageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $products = Product::inRandomOrder()->take(10)->get();

        // return view('pages.index')->with('index', $products);
        $products = Product  ::inRandomOrder()->take(9)->get();
        return view ('pages.index')->with('index',$products);

    }
}
Sethu

You're using index as key on with(), I hope you've used {{ $products }} on view, so you should replace index with products on with() just like below

return view('pages.index')->with('products', $products);

OR

Instead of using with() you can pass the data as array to the view() just like below and you can use the corresponding key inside the view

return view('pages.index', ['products' => $products]);

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related