【问题标题】:Laravel pass collection to component [duplicate]Laravel将集合传递给组件[重复]
【发布时间】: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' =&gt; $match] 而不仅仅是$match?应该是一个数组,因为您可以向组件添加多个数据。 laravel.com/docs/5.8/blade#components-and-slots
  • 谢谢@kerbholz 我没有正确传递数组是我的错!谢谢!
  • @kerbholz 可能会将其作为答案发布,以便可以将问题标记为已正确解决

标签: laravel laravel-5 eloquent laravel-blade


【解决方案1】:

组件的参数需要是键 => 值对的数组。

将您的单个参数 $match 替换为 ['match' =&gt; $match] 数组。 (如果您的组件中需要更多参数,可以将它们添加到数组中)

<div class="row">
    @foreach($matchs as $match)
        @component('pages.processes.offerdemand.matchs.matchbox', ['match' => $match])
        @endcomponent
    @endforeach
</div>

【讨论】:

    猜你喜欢
    • 2020-12-24
    • 2021-07-15
    • 2018-09-06
    • 2019-10-01
    • 2018-07-14
    • 2017-03-06
    • 2021-11-11
    • 2011-06-02
    • 2012-03-01
    相关资源
    最近更新 更多