外观\点火\异常\视图异常错误在拉拉维尔?



0

我到达了一个我无法弄清楚的部分。尝试在主页上显示产品时,出现以下错误:

Undefined variable: products (View: /home/acer/test/project_basket/basket/resources/views/home.blade.php)

对我来说是php的第一个项目,我对这种语言不是很练习。

首页刀片.php:

@section('content')
<div class="card-deck">

/*Problem Here */
@foreach ($products as $product)
<div class="card">
<img src="{{ $product->imagePath }}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ $product->title }}</h5>
<p class="color">{{ $product->color }}</p>
<a href="#" class="btn btn-danger">Buy Now</a>
<button type="button" class="btn btn-primary float-right">Add to Cart</button>
<div class="price">${{ $product->price }}/div>
</div>
</div>
@endsection

产品.php

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Product extends Model
{
protected $fillable = ['imagePath', 'title', 'price', 'color'];
}

产品控制器.php:

<?php
namespace AppHttpControllers;
use AppProduct;
use IlluminateHttpRequest;
//use IlluminateHttpRequest;
class ProductController extends Controller
{
/**
*@return IlluminateHttpResponse
*/
public function index()
{
$products = Product::inRandomorder()->take(6)->get();
return view('home')->with('products', $products);
}
}

路线:

//Route::view('/`home`', 'home');

Route::get('/', 'ProductController@index')->name('home');
Auth::routes();
Route::get('/home', 'ProfilesController@index')->name('home');
Route::get('/', 'ProfilesController@index')->name('welcome');
//Route::get('/home', 'DasboardController@index')->name('dashboard');

您正在"命中"/home端点,该端点查看您的路由指向ProfilesController,但您正在处理ProductController所以在web.php中编写了错误的控制器

最新更新