'How can I pass the list to the component variable in Laravel?
How can I pass the list to the component variable? I tried to add elements of a list in @php
tags like this , for now,, in the future, I will fetch this list from the database. What am I doing wrong here?
htmlspecialchars(): Argument #1 ($string) must be of type string, array given
Component in my blade file looks like this
resource/views/pricing.blade.php
<div>
@php
$allowedFeaturesList = ["a","b","c"];
@endphp
<x-widgets.pricingCard name="Free" allowedFeaturesList="{{$allowedFeaturesList}}"/>
</div>
resources\views\components\widgets\pricingCard.blade.php
<div class="card-body p-5">
<div class="small text-uppercase fw-bold text-muted">{{$name}}</div>
<ul class="list-unstyled mb-4">
<!-- Below is how I'm trying to render list -->
@foreach ($allowedFeaturesList as $item)
<li class="mb-2">
<i class="bi bi-check text-primary"></i>
{{$value}}
</li>
@endforeach
</ul>
</div>
app\View\Components\widgets\pricingCard.php
<?php
namespace App\View\Components\widgets;
use Illuminate\View\Component;
class pricingCard extends Component
{
public $name;
public Array $allowedFeaturesList;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($name,$price,$booledFeature,$allowedFeaturesList,$restrictedFeaturesList,)
{
$this->name= $name;
$this->allowedFeaturesList= $allowedFeaturesList;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.widgets.pricingCard');
}
}
Solution 1:[1]
You can try the following:
On your view component tag
<x-widgets.pricingCard name="Free" allowedFeaturesList="{!! json_encode($allowedFeaturesList) !!}}"/>
On pricingCard.php file
{ $this->name= $name; $this->allowedFeaturesList= json_decode($allowedFeaturesList); }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shree Sthapit |