1

I've made controller, model and view. I've included compact in the controller for the variable but getting the error

Undefined variable: products (View: C:\xampp\htdocs\laravelapps\coffe\resources\views\shop.blade.php),Possible typo $products Did you mean $errors?)

controller

namespace App\Http\Controllers;

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

class ProductController extends Controller
{
    public function getIndex()
    {
        $products = Product::all();
        dd($products);
        return view('shop', compact('products'));
    }
} 

model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['imagePath', 'title', 'description', 'price'];
}

view

@foreach($products->chunk(3) as $productChunk)
    <div class="row">
        @foreach($productChunk as $product)
            <div class="col-md-3">
                <div class="menu-entry">
                    <a href="#" class="img" style="background-image: url(images/menu-1.jpg);"></a>
                    <div class="text text-center pt-4">
                        <h3><a href="product-single.html">Coffee Capuccino</a></h3>
                        <p>A small river named Duden flows by their place and supplies</p>
                        <p class="price"><span>$5.90</span></p>
                        <p><a href="http://localhost/laravelapps/coffe/public/cart"
                              class="btn btn-primary btn-outline-primary">Add to Cart</a></p>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
@endforeach

route

Route::get('/', ['uses' => 'ProductController@getIndex', 'as' => 'product.index']);
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
Marwa
  • 31
  • 3
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – B001ᛦ Aug 13 '20 at 10:30
  • i forgot the controller ` – Marwa Aug 13 '20 at 10:30
  • 1
    Welcome to SO ... what URL is in the address/location bar when you get this error? it is possible you are returning this same view somewhere else – lagbox Aug 13 '20 at 10:58
  • http://localhost/laravelapps/coffe/public/shop – Marwa Aug 13 '20 at 12:02
  • Is the code you have shown as view actually the contents of `C:\xampp\htdocs\laravelapps\coffe\resources\views\shop.blade.php`? – rineez Aug 13 '20 at 17:51
  • And.. that "route" you have pasted in the question doesn't look like the route entry for URL `/shop` – rineez Aug 13 '20 at 17:53
  • how can i fix it then!! – Marwa Aug 17 '20 at 09:01

0 Answers0