我到达了一个我不能理解的部分。当尝试在主页上显示产品时,我得到以下错误:
Undefined variable: products (View: /home/acer/test/project_basket/basket/resources/views/home.blade.php)
对我来说是php的第一个项目,我并不是很熟悉这门语言。
home.blade.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>
@endsectionProduct.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['imagePath', 'title', 'price', 'color'];
}ProductController.php:
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
//use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
*@return \Illuminate\Http\Response
*/
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');发布于 2020-06-27 19:46:55
您正在“访问”/home端点,该端点查看您的路由指向ProfilesController,但是您正在处理ProductController,因此在web.php中编写了错误的控制器
https://stackoverflow.com/questions/62603391
复制相似问题