简体   繁体   中英

laravel eloquent using a json object to search and array in my database

I have a json object

'developer' => 
  array (
    'count' => 1,
    'docPosition' => 100,
    'countCv' => 1,
    'weight' => '0.077',
  ),
  'Software Engineer' => 
  array (
    'count' => 4,
    'docPosition' => 716,
    'countCv' => 4,
    'weight' => '0.308',
  ),
  'engineer' => 
  array (
    'count' => 5,
    'docPosition' => 725,
    'countCv' => 5,
    'weight' => '0.385',
  ),
  'Software Development Engineer' => 
  array (
    'count' => 1,
    'docPosition' => 1272,
    'countCv' => 1,
    'weight' => '0.077',
  ),
  'Development Engineer' => 
  array (
    'count' => 1,
    'docPosition' => 1281,
    'countCv' => 1,
    'weight' => '0.077',
  ),
  'Contract' => 
  array (
    'count' => 1,
    'docPosition' => 1303,
    'countCv' => 1,
    'weight' => '0.077',
  ),
)

I want to get the keys of the object (developer, Software Engineer, engineer, etc) and use them to search the db for matches that are in a field called Industry that is an array. I am unsure about the following.

  1. How to get the keys on the object and turn it into an array.
  2. Search my db for any matches between the array I created and the array in the field Industry.

I have searched google and stack and have not found anything helpful. Any ideas or strategies would be appreciated.

You can make that json object into a Collection :

$collection=collect(json_decode($yourObject);

And get the keys :

$keys = $collection->keys();

And you can retrieve from your data like this

$data = collect($yourData);
$results = $keys->each(function($key){
             $data->only($key);
             //do your logic with it
});

you can learn more about it here : Laravel Collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM