Laravel,如何按标题将数据存储在数组中 [英] Laravel, How to store items in array grouped by their title

查看:68
本文介绍了Laravel,如何按标题将数据存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里多次问了这个问题,因为到现在为止还有4天没有得到正确的答案,我想存储产品及其修饰符,这些修饰符的标题为:如果需要,请告诉我们",每个修饰符及其标题和产品存储在数据库中,因此我想在每个产品下方显示标题及其修饰符,该标题和修饰符可以重复,这意味着可能有很多带有修饰符的标题,因此我想通过将其存储到数组,我尝试了以下尝试帮助我的代码,但他只是消失了,但此代码无法完美运行.希望您能得到我想要的东西,并帮助我解决这个问题,在此先感谢您

I asked this question many times here, since 4 days till now didn't get the correct answer, I want to store product and their modifiers, these modifiers have title such as: "TELL US IF YOU'LL NEED", every modifier store in database with its title and its product, so I want to show under every product the title and its modifier which may be repeated, mean may be there are many titles with thier modifiers, so I want to do that by storing into array, I tried the code below from some one tried to help me but he just disapeared but this code doesnt work perfectly. Hope you got what I want and help me to solve this issue, thank you in advance

  $customizeorders = OrderCustomize::where('userorder_id',$order_number)->with('customizeproduct')->get();

      $customizes = [];
      $wrapper = [];

$previousTitle = $customizeorders[0]->customizeproduct->customizetitle->name;

  foreach ($customizeorders as $data) {
$singleItem = [];

if ($data->customizeproduct->customizetitle->name == $previousTitle) 
{

  $title = $data->customizeproduct->customizetitle->name;
  $product=$data->product_id;
  $modifiers=$data->customizeproduct->customize_title;

            array_push($singleItem, $title,$product,$modifiers);
            array_push($wrapper, $singleItem);
        } 


        else  {               
      $previousTitle = $data->customizeproduct->customizetitle->name;
    array_push($customizes, $wrapper);
      $wrapper= [];                   
               } 
         }

  dd($customizes) ;

结果如下:

  array:4 [▼
  0 => array:1 [▼
0 => array:3 [▼
  0 => "TELL US IF YOU'LL NEED"
  1 => 2606
  2 => "Napkins"
   ]
   ]
   1 => array:5 [▼
0 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2610
  2 => "Bacon"
   ]
1 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2610
  2 => "Onions"
     ]
  2 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2610
  2 => "Hot Giardiniera"
  ]
3 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2610
  2 => "Garlic"
  ]
4 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2610
  2 => "BBQ Sauce"
 ]
 ]
2 => []
 3 => array:3 [▼
0 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2618
  2 => "Anchovies"
]
1 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2618
  2 => "Black Olives"
]
2 => array:3 [▼
  0 => "ADD TOPPINGS"
  1 => 2618
  2 => "Pepperoni"
]
]
]

推荐答案

因此,您需要按customproduct-> customizetitle-> name包含订单,以便获得序列中的所有产品.然后该功能将起作用.该函数的主要功能是,如果序列看起来像这样,将创建项目的包装.

Bro you need to include order by customizeproduct->customizetitle->name so that you get all the products in the sequence. And then that function will work. The key feature of the function is that will create the wrapper of the item if the sequence looks like this.

1st: TELL US IF YOU'LL NEED
2nd: TELL US IF YOU'LL NEED
3rd: TELL US IF YOU'LL NEED
4th: TELL US IF YOU'LL NEED
5th: TELL US
6th: TELL US
7th: TELL US
8th: Hey You
9th: Hey You
10th: Hey You

然后它将创建多维数组,如下所示:

then it will create the multidimensional array like:

[[1st,2nd,3rd,4th],[5th,6th,7th],[8th,9th,10th]]

然后您可以在刀片中的foreach循环内使用foreach循环以获取所需的输出

then you can use foreach loop inside foreach loop in the blade to get the desired output

这篇关于Laravel,如何按标题将数据存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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