Search code examples
phparraysforeachlaravel-5.3

How to array data from @foreach in Laravel Views


I know this is possible but I've tried to search from the net but I've never found an answer so if anyone who knows how to push data to array from @foreach using Laravel View please help me. Thanks in advance

Here is the code

@foreach ($fields as $key => $rel)

 {{ $arrayData = array(

    'field_type' =>'$rel->field_type'
 )}}

@endforeach

The error I've receiving is

ErrorException in helpers.php line 519: htmlspecialchars() expects parameter 1 to be string, array given (View: C:\xampp\htdocs\fields.blade.php)


Solution

  • Remove quote from $rel->field_type

    @foreach ($fields as $key => $rel)
    
     {{ $arrayData = array(
    
        'field_type' =>$rel->field_type
     )}}
    
    @endforeach