H
H
HeartOfProgrammer2016-05-08 17:33:47
Laravel
HeartOfProgrammer, 2016-05-08 17:33:47

Why is the product variable not defined error?

On this path to the site localhost:8000/index gives an error


Undefined variable: product (View: D:\OpenServer\domains\sportgoods\resources\views\index.blade.php)

This is my entire controller.
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Product;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products = Product::all();
        return view('products.index', compact('products'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('products.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $product = Request::all();
        Product::create($product);

        return redirect('products');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $product = Product::find($id);
        return view('products.show', compact('product'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product = Product::find($id);

        return view('products.edit', compact('product'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $productUpdate = Request::all();
        $product = Product::find($id);
        $product->update($productUpdate);

        return redirect('products');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Product::find($id)->delete();

        return redirect('products');
    }
}

And here is the block on which he swears
<div class="col-sm-3 col-md-3">
<div class="block thumbnail">
<img src="img/product.png" alt="..." class="image-responsive">
<div class="caption">
 <h3>Thumbnail label</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid delectus facere harum in ipsam perferendis quisquam. Culpa earum, excepturi exercitationem illum ipsam minus nobis perspiciatis provident quasi recusandae rerum voluptatem.</p>
@if(Auth::check() && (Auth::user()->is_admin))
    <a href="{{ route('products.edit', $product->id) }}" class="btn btn-success">Редактировать</a>
    <a href="{{ route('products.destroy', $product->id) }}" class="btn btn-danger">Удалить</a>       
@endif
</div>
</div>
 </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav, 2016-05-08
@hedint

In your controller, in the index method, you are passing a collection of products into the template, and in the template you are trying to access some kind of product?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question