How to optimize code in Laravel?

前端 未结 1 1569
轻奢々
轻奢々 2021-01-16 18:48

I use the following code to get data from two related tables:

$arr = [];
$objectModel = new ProductCategory();
$objectModel::$language = 2;

$subcategories =         


        
相关标签:
1条回答
  • 2021-01-16 19:04

    You can use Laravel Collections https://laravel.com/docs/5.3/collections

    $arr = ProductCategory::with("translate", "parent")->get()
                ->mapWithKeys(function ($item, $key) {
                    return [$item->translate()->first()->objectId => $item->translate()->first()->name];
                })
                ->all();
    
    0 讨论(0)
提交回复
热议问题