“未定义变量:书籍

时间:2018-07-26 08:10:16

标签: laravel laravel-5 laravel-4 eloquent

我收到此错误:

  

“未定义变量:书籍(查看:   C:\ xampp \ htdocs \ Shop \ resources \ views \ welcome.blade.php)“

我在Welcome.blade.php中显示商品时遇到了麻烦,因为它说我有一个未定义的变量。而且我找不到使它工作的方法。

这是我的模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name',
        'description',
        'condition',
        'category_id',
        'image'
    ];

    public function category()
    {
        $this->belongsTo(Category::class);
    }
}

这是我的 ProductsController

 namespace App\Http\Controllers;

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

class ProductsController extends Controller
{
    public function index()
    {
        $products = Product::all();
        return view('admin.product.index',compact('products'));
    }

    public function create()
    {
        $categories = Category::pluck('name','id');
        return view ('admin.product.create', compact('categories'));
    }


    public function store(Request $request)
    {
        $product = $request->except('image');

        //validation
        $this->validate($request,[
            'name' => 'required',
            'condition' => 'required',
            'image' => 'image|mimes:png,jpg,jpeg|max:10000'
        ]);


        //image upload
        $image = $request->image;
        if( $image ){
            $imageName=$image->getClientOriginalName();
            $image->move('images',$imageName);
            $product['image']=$imageName;
        }

        Product::create($product);
        return redirect()->route('admin.index');
    }
}

这是我的 Welcome.blade.php

<div class="row">
    @forelse( $books as $book)
        <div class="column-prod">
            <a href="{{route('book')}}">
                <div class="card-prod">
                    <center>
                        <img src="/css/aa.jpg"  style="width: 300px; height: 300px;">
                    </center> 
            </a>
            <div class="container-prod">
                <h2></h2>
                <p class="title">{{$book->name}}</p>
                <p class="price">Php 450.00</p>

                <p>
                    <button class="button">
                        <i class="fas fa-cart-arrow-down"></i>Add to Cart
                    </button>
                </p>

                <p>
                    <button class="button">
                        <i class="fas fa-heart"></i>  Add to Wishlist
                    </button>
                </p>
            </div>
        </div>
    </div>

    @empty
         <h3> No Books </h3>
    @endforelse

</div> <!--Div all end-->
@endsection

这是我的web.php,很抱歉,我是新来的...希望您能为我提供帮助。

<?php
Route::get('/', function () {
    return view('welcome');
});

Route::get('/cart','ShopController@cart');

Route::get('/signin','ShopController@signin');

Route::get('/book','ShopController@book');

Route::get('/wishlist','ShopController@wish');



Auth::routes();

Route::get('/logout', 'Auth\LoginController@logout');

Route::get('/home', 'HomeController@index');

Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){
    Route::get('/',function(){
        return view('admin.index');
    })->name('admin.index');


    Route::resource('product','ProductsController');
    Route::resource('category','CategoriesController');
});

2 个答案:

答案 0 :(得分:0)

替换

Route::get('/', function () {
return view('welcome');
});

使用

Route::get('/', function () {
    return view('welcome')->with(['books' => \App\Books::all()]);
});

(如果这可行,那么您应该像其余部分一样将代码移入控制器)

答案 1 :(得分:0)

替换

<div class="container text-center">
    <h2>Jquery - Timepicker Example using jQuery Timepicker Plugin</h2>
    <strong>Select Time:</strong> <input type="text" id="timepicker" class="from-control">
</div>


<script type="text/javascript">
    $("#timepicker").timepicker(
    {
        timeFormat: 'H:mm ',
        interval: 1,
        minTime: '12am',
        maxTime: '23.59',
        startTime: '12am',
        dynamic: true,
        dropdown: true,
        scrollbar: true
    });


</script>

Route::get('/', function () {
return view('welcome');
});