0

I have a view which has a component for showing house properties information.

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

In my Controller i'm returning a collection and each "matchbox" is created OK depending on the total elements of the collection.

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'));

    }
}

The problem i'm facing is that I want to pass variable $match to each box, it looks like components only accept an array and not a collection.

<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 (

How can I pass the data to the component box?

Regards

Belisario Peró
  • 609
  • 1
  • 11
  • 22
  • 1
    Use the collection's `toArray()` method? – miken32 Mar 04 '20 at 22:01
  • Thanks @miken32, I tried your suggestion but it's also failing: `Facade\Ignition\Exceptions\ViewException Argument 2 passed to Illuminate\View\Factory::startComponent() must be of the type array, object given` – Belisario Peró Mar 04 '20 at 22:07
  • 2
    Have you tried `['match' => $match]` instead of just `$match`? Should be an array because you can add multiple data to the component. https://laravel.com/docs/5.8/blade#components-and-slots – brombeer Mar 04 '20 at 22:08
  • Thanks @kerbholz I was my fault not passing the array correctly! Thanks! – Belisario Peró Mar 04 '20 at 22:13
  • @kerbholz maybe post that as an answer so the question can be marked as solved correctly – mdexp Mar 05 '20 at 00:24

1 Answers1

3

The parameters for a component need to be an array of key => value pairs.

Replace your single parameter $match with an array of ['match' => $match]. (If you need more parameters in your component you can add them to the array)

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