H
H
HeartOfProgrammer2016-02-28 19:59:10
Laravel
HeartOfProgrammer, 2016-02-28 19:59:10

Why is variable not defined in laravel 5.1?

I can’t understand why laravel swears that the variable is not defined
b40f5921e36e49f7828a58fcd04415d6.png
The whole code of the productList.blade.php file:

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="btn btn-block"><a href="{{ route('product.create')  }}"><h2>Add New Product</h2></a></div>
    @foreach( $allProducts as $product )
        <hr>
        <div class="row">
            {!! HTML::linkRoute('product.edit', 'Edit', array($product->id), ['class'=>'btn btn-default col-xs-2']) !!}
            {!! Form::open(['route' => ['product.destroy', $product->id], 'method' => 'delete']) !!}
            <input class="btn btn-default col-xs-2" type="submit" value="Удалить" />
            {!! Form::close() !!}
        </div>
        <div><h1>{!! HTML::linkRoute('product.show', $product->title, array($product->id)) !!}</h1></div>
        <div>{{ $product->description }}</div>
        <div class="pull-right"><em>{{'Опубликовал -'. $product->author }}</em>
        </div>
    @endforeach
    {!! Form::open(['route' => ['product.destroy', $product->id], 'method' => 'delete']) !!}
    <input class="btn btn-default col-xs-2" type="submit" value="Удалить" />
    {!!Form::close() !!}
</div>
</body>
</html>

My controller:
public function index()
    {
        $allProducts = Product::all();

        return view('products.productList', compact('allProducts'));
    }

Model:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'products';
    protected $fillable = [
        'title',
        'description',
        'author',
        'image'
    ];
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Muhammad, 2016-02-28
@HeartOfProgrammer

You are trying to use the $product variable behind a loop. Need like this:

<div class="pull-right"><em>{{'Опубликовал -'. $product->author }}</em> </div>

    {!! Form::open(['route' => ['product.destroy', $product->id], 'method' => 'delete']) !!}
        <input class="btn btn-default col-xs-2" type="submit" value="Удалить" />
    {!!Form::close() !!}
@endforeach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question