Laravel split in blade pattern with nested foreach

I am retrieving an array of space-separated IDs as shown below.

  @foreach($user_notify->user_notify_divisions as $user_notify)
                    {{$user_notify}}
  @endforeach

      

Then the value of $ user_notify will be the identifier

   $user_notify = 2 3 4 5

      

I have a database table section_list

            id        division_name
             1         工業製品卸・小売
             2         繊維・衣料製造
             3         繊維・衣料卸・小売
             4         食品製造
             5         食品卸・小売
             6         医薬品製造

      

Now how to get the division_name with the above id string

Thanks everyone!

+3


source to share


3 answers


As laravel

blade templating

you can do this as follows: nested foreach

.

Another problem in your code: you are iterating over $user_notify->user_notify_divisions

and define the value as $user_notify

, you should have a different variable name instead of the same name as for the object

@foreach($user_notify->user_notify_divisions as $user_notify)
                    {{$user_notify}}
@endforeach

      



Corrected code:

@foreach($user_notify->user_notify_divisions as $user_data)
    @foreach(explode(" ",$user_data) as $ids)
          {{$ids}}
    @endforeach
@endforeach

      

+2


source


you can use explode()

to split a string.



$arr = explode(' ', $user_notify);

      

+1


source


You can use explode

or direct access by ID.

$id_array = [2,3,4,5];
dd($id_array[0]); // results 2

      

0


source







All Articles