Ad

String Append In For Each Loop On Laravel

- 1 answer

I'm using a for each loop and fetching the value into the variable crosslisted_courses and also appending the next values in the loop into it, but the value does not gets appended!! The variable only stores the last fetched value

Have used .= to append the string !!

@foreach($fmfacactinss as $fmfacactins_slave)                                                                                                                                              

@if(($fmfacactins_slave->xlist_ctrl_crn == $master_crn) && ($fmfacactins_slave->crn <> $master_crn) ) 
@php  
$crosslisted_courses = '';                                                                                
$slave_crn=$fmfacactins_slave->crn;                                                                                
$crosslisted_courses .= $fmfacactins_slave->crs_subj_cd . ' ' . 
$fmfacactins_slave->crs_nbr . ' ' . $fmfacactins_slave->sect_nbr . ' ';                                                                                
echo($crosslisted_courses);                                                                                
@endphp
@endforeach
Ad

Answer

I'm not familiar to Laravel syntax, but I think you should declare $crosslisted_courses before the loop.

@php  
$crosslisted_courses = '';
@endphp

@foreach($fmfacactinss as $fmfacactins_slave)

@if(($fmfacactins_slave->xlist_ctrl_crn == $master_crn) && ($fmfacactins_slave->crn <> $master_crn) ) 
@php
$slave_crn=$fmfacactins_slave->crn;
$crosslisted_courses .= $fmfacactins_slave->crs_subj_cd . ' ' . 
    $fmfacactins_slave->crs_nbr . ' ' . $fmfacactins_slave->sect_nbr . ' ';                                                                                
echo($crosslisted_courses);
@endphp

@endforeach
Ad
source: stackoverflow.com
Ad