简体   繁体   中英

Laravel split in blade template nested foreach

I retrieve an array of IDs separated by white space as shown below

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

Then the value of $user_notify will be the ID

   $user_notify = 2 3 4 5

I have a database table division_list

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

Now how to get division_name with the above id string

Thanks everyone!

In laravel blade templating you can do it like this nested foreach .

Another problem in your code is, you are iterating on $user_notify->user_notify_divisions and defining value as $user_notify you should have different variable name instead of same name as of 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

你可以使用explode()来分割字符串。

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

You can use explode or directly access by id.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM