Laravel组件未显示



我正在学习laravel,我正在尝试使用一些组件。

我已经创建了一个布局组件,其中包括导航栏,它是正确的工作。

我仍然在控制器中使用键/值数组,因为我还没有研究数据库。

现在我试图创建一个动态卡片组件,我在一个名为card.blade.php的新组件中编写了以下内容:

<div class="card" style="width: 18rem;">
<img class="card-img-top" src="{{$img}}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{$title}}</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>

在我想要显示这些卡片(shop.blade.php)的页面上,我做了以下操作:

<x-layout>
<div class="container">
<div class="row">
@foreach($products as $product)
<div class="col-4">
<x-card
img="{{$product['img']}}"
title = "{{$product['name']}}"
/>
</div>
@endforeach
</div>
</div>
</x-layout>

但是当我运行本地主机时,这些卡没有在浏览器上呈现,或者更好的是,它们没有任何大小(0x0)。我可以从'Inspect'中看到它正在接受正确的参数,并且也从forEach中正确生成。

我试着运行相同的,但没有任何动态变量和卡显示正确与他们的大小。

这就像当我试图将变量关联起来时,它们失去了维度。

有人能帮我处理这件事吗?: DThank you so much

组件由两部分组成:一个类和一个视图。

确保在App/View/Components文件夹中创建了一个名为Card的类。如果是,检查title和img属性是否存在,并且它们是公共的。

App/View/Components/Card.php:


namespace AppViewComponents;
use IlluminateViewComponent;
class Card extends Component
{
public $img;
public $title;
public function __construct($img, $title)
{
$this->img = $img;
$this->title = $title;
}
public function render()
{
return view('components.card');
}
}