【发布时间】:2020-06-17 11:49:05
【问题描述】:
我有一个视图,其中包含用于显示房屋属性信息的组件。
<div class="row">
@foreach($matchs as $match)
@component('pages.processes.offerdemand.matchs.matchbox')
@endcomponent
@endforeach
</div>
在我的控制器中,我返回一个集合,每个“火柴盒”都根据集合的总元素创建好。
class OfferdemandsmatchsController extends Controller
{
public function index ($id) {
$matchs = DB::table('offerdemandsmatchs')
->join('properties', 'offerdemandsmatchs.prop_id', '=', 'properties.prop_id')
->where('offerdemandsmatchs.offerdemand_id', '=', $id)
->select('properties.*', 'offerdemandsmatchs.created_at as matchcreated_at', 'offerdemandsmatchs.created_at as matchupdated_at',
'offerdemandsmatchs.like', 'offerdemandsmatchs.id as matchid')
->get();
return view('pages.processes.offerdemand.matchs.index', compact('matchs'));
}
}
我面临的问题是我想将变量 $match 传递给每个盒子,看起来组件只接受一个数组而不是一个集合。
<div class="row">
@foreach($matchs as $match)
@component('pages.processes.offerdemand.matchs.matchbox', $match)
@endcomponent
@endforeach
</div>
Facade\Ignition\Exceptions\ViewException
Argument 2 passed to Illuminate\View\Factory::startComponent() must be of the type array, object given, called in C:\Desarrollo\laragon\www\kw-plataforma\storage\framework\views\60fac8943d076d952347a84172f358cd1ee65f54.php on line 39 (
如何将数据传递给组件框?
问候
【问题讨论】:
-
使用集合的
toArray()方法? -
谢谢@miken32,我尝试了你的建议,但也失败了:
Facade\Ignition\Exceptions\ViewException Argument 2 passed to Illuminate\View\Factory::startComponent() must be of the type array, object given -
您是否尝试过
['match' => $match]而不仅仅是$match?应该是一个数组,因为您可以向组件添加多个数据。 laravel.com/docs/5.8/blade#components-and-slots -
谢谢@kerbholz 我没有正确传递数组是我的错!谢谢!
-
@kerbholz 可能会将其作为答案发布,以便可以将问题标记为已正确解决
标签: laravel laravel-5 eloquent laravel-blade