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

如何从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.

  • 写回答

2条回答 默认 最新

  • duanfu1942 2019-01-29 09:50
    关注

    You can use laravel collection helpers :

    Business::whereIn('category_id', collect($lev_cat3)->flatten()->all())->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)