laravel刀片中的一个Foreach循环中的多个变量传递 [英] Multiple variable pass in one Foreach loop in laravel blade

查看:80
本文介绍了laravel刀片中的一个Foreach循环中的多个变量传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有两个变量:

I have two variables in a controller:

$data = S_core_Country::all();
$cn=\DB::table('s_user_addresses')
   ->join('s_users', 's_users.id', '=', 's_user_addresses.user_id')
   ->join('s_core_countries', 's_core_countries.id', '=', 's_user_addresses.country_id')
   ->value('countryname');                                       

return view('home')->with(['data'=>$data,'cn'=>$cn]);

是否可以像这样在foreach循环中查看我的数据

Is it possible to view my data in foreach loop like this

@foreach($data as $data && $cn as $cn)

如果不是,我应该在一个foreach循环中显示那两个变量吗?

If not what should I to show those two variable in one foreach loop?

推荐答案

要合并和合并数组,可以尝试:

For merge and combine array , you can try :

$array = array_merge($array1,$array2);

刀片中的示例:

@foreach(array_merge($array1,$array2) as $item)
    // ...
@endforeach


另一个例子:


Another Example :

@foreach($array1 as $key => $row)
    <li>
        <b>{{ $row['id'] }}</b>
        <p>{{ $array2[$key]->payment }}</p>
    </li>
@endforeach


相关链接:


Related Links :

https://codereview.stackexchange.com/Questions/70419/使用嵌套的foreach循环合并两个数组

https://knackforge.com/blog/sabareesh/iterate-2-arrays-single-foreach-loop

合并两个数组

http://php.net/manual/zh/function.array-merge.php

https://www.w3schools.com/php/func_array_merge.asp

https://www.w3schools.com/php/showphp.asp?filename = demo_func_array_merge

https://laravel.com/docs/5.7/blade#the-loop-variable

&&运算符(和)是逻辑运算符.

The && operator(and) is a logical operator.

$ a&&$ b如果$ a和$ b都为TRUE,则为TRUE.

$a && $b And TRUE if both $a and $b are TRUE.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆