dtkjthe4025
2019-01-29 09:39
采纳率: 0%
浏览 2.1k
已采纳

如何从Laravel中的数组中获取值

I have a 3 level category. I need to gain businesses on level 3 category.

I write this code:

$main_cat = Category::where(['slug' => $url])->first();
$lev_cat2 = Category::where(['parent_id' => $main_cat->id, 'status' => '1'])->get();
 foreach ($lev_cat2 as $subCategory) {
            $cat_ids_lv2[] = $subCategory->id . ',';
        }
 foreach ($lev_cat2 as $subCat) {
            $lev_cat3[] = Category::where(['parent_id' => $subCat->id, 'status' => '1'])->pluck('id')->toArray();
        }

dd($lev_cat3);

Then I got this array which is correct:

array:5 [▼
  0 => array:3 [▼
    0 => 145
    1 => 146
    2 => 147
  ]
  1 => array:3 [▼
    0 => 148
    1 => 149
    2 => 150
  ]
  2 => array:3 [▼
    0 => 151
    1 => 152
    2 => 153
  ]
  3 => array:3 [▼
    0 => 154
    1 => 155
    2 => 156
  ]
  4 => []
]

now I dont know how can I get values like 145,146,147,148,149,... to pass theme to

Business::where(['category_id'=> [145,146,147,148,...]]->get();

of course dynamic.

图片转代码服务由CSDN问答提供 功能建议

我有一个3级别的类别。 我需要获得3级类别的业务。</ p>

我写这段代码:</ p>

  $ main_cat = Category :: where(['  slug'=&gt; $ url]) - &gt; first(); 
 $ lev_cat2 = Category :: where(['parent_id'=&gt; $ main_cat-&gt; id,'status'=&gt;'1']  ) - &gt; get(); 
 foreach($ lev_cat2 as $ subCategory){
 $ cat_ids_lv2 [] = $ subCategory-&gt; id。  ','; 
} 
 foreach($ lev_cat2 as $ subCat){
 $ lev_cat3 [] = Category :: where(['parent_id'=&gt; $ subCat-&gt; id,'status'=&gt;  '1']) - &gt; pluck('id') - &gt; toArray(); 
} 
 
dd($ lev_cat3); 
 </ code> </ pre> 
 
 

然后我得到了这个正确的数组:</ p>

  array:5 [▼
 0 =&gt; 数组:3 [▼
 0 =&gt;  145 
 1 =&gt;  146 
 2 =&gt;  147 
] 
 1 =&gt; 数组:3 [▼
 0 =&gt;  148 
 1 =&gt;  149 
 2 =&gt;  150 
] 
 2 =&gt; 数组:3 [▼
 0 =&gt;  151 
 1 =&gt;  152 
 2 =&gt;  153 
] 
 3 =&gt; 数组:3 [▼
 0 =&gt;  154 
 1 =&gt;  155 
 2 =&gt;  156 
] 
 4 =&gt;  [] 
] 
 </ code> </ pre> 
 
 

现在我不知道如何获得像145,146,147,148,149,...这样的值来将主题传递给</ p> <n pre> Business :: where(['category_id'=&gt; [145,146,147,148,...]] - &gt; get(); </ code> </ pre>

of 当然是动态的。</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题