Laravel智能阵列匹配

时间:2017-07-01 07:25:59

标签: php arrays laravel-5 relationship

我的laravel项目中有很多关系。 我开始搜索了。现在我有更多的多维数组,我需要检查所有数组值是否可用?! 所以我的阵列:

array:4 [▼
  "bytype" => array:2 [▼
    0 => array:6 [▼
      "id" => 1
      "price" => 500
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
    1 => array:6 [▼
      "id" => 3
      "price" => 1400
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
  ]
  "bymake" => array:2 [▼
    0 => array:6 [▼
      "id" => 1
      "price" => 500
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
    1 => array:6 [▼
      "id" => 3
      "price" => 1400
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
  ]
  "bymodel" => array:1 [▼
    0 => array:6 [▼
      "id" => 2
      "price" => 1500
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
  ]
  "byattribute" => array:3 [▼
    0 => array:6 [▼
      "id" => 1
      "price" => 500
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
    1 => array:6 [▼
      "id" => 2
      "price" => 1500
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
    2 => array:6 [▼
      "id" => 3
      "price" => 1400
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
  ]
]

那么我怎么能做到这一点来获得那个可以在所有4个主数组中使用的值(它意味着数组)?

就是这样:

  "listable" => array:2 [▼
    0 => array:6 [▼
      "id" => 1
      "price" => 500
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
    1 => array:6 [▼
      "id" => 3
      "price" => 1400
      "discount_price" => null
      "created_at" => null
      "updated_at" => null
      "pivot" => array:2 [▶]
    ]
  ]

- EDIT-- 我的搜索控制器:

$original=$resquest->all();
        $searchableAttributes=$resquest->all();
        if(isset($resquest->type)){
            $type=VehicleType::find($resquest->type);
            $vehicles['bytype']=$type->vehicle()->toArray();
            unset($searchableAttributes['type']);
        }
        if(isset($resquest->make)){
            $make=VehicleMake::find($resquest->make);
            $vehicles['bymake']=$make->vehicle->toArray();
            unset($searchableAttributes['make']);
        }
        if(isset($resquest->model)){
            $model=VehicleModel::find($resquest->model);
            $vehicles['bymodel']=$model->vehicle->toArray();
            unset($searchableAttributes['model']);
        }
        if(!empty($searchableAttributes)){
            foreach ($searchableAttributes as $key => $value) {
                $attribute=VehicleAttributeValue::find($value);
                $vehicles['byattribute']=$attribute->vehicle->toArray();

            }
        }

车型型号:

public function vehicle(){
        return $this->belongsToMany('App\Vehicle','vehicles_types_vehicles','vehicle_type_id','vehicle_id');
    }

以及所有3个这样的模型,具有不同的键和表

0 个答案:

没有答案