0
$est_data = Establishments::where('status',0)->where('city','=',$data['location'])->get(array('id'));
return $est_data;

result:

[{"id":43},{"id":71},{"id":41},{"id":39}]

This is my above laravel condition and result, i want the result to be like 43,71,41,39. Can anyone please help me, how can be this done using php. i tried with implode() function, but getting error, please help...thank you

brombeer
  • 8,716
  • 5
  • 21
  • 27
Sonu
  • 191
  • 1
  • 10
  • did you see https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array? – Aleksandrs Oct 30 '18 at 08:08
  • Welcome. "_but getting error_" What would that error be? – brombeer Oct 30 '18 at 08:10
  • 1
    Possible duplicate of [How to Flatten a Multidimensional Array?](https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – Gabriel M Oct 30 '18 at 08:10
  • $est_data = Establishments::where('status',0)->where('city','=',$data['location'])->get(array('id')); return $implode = implode(' ',$est_data); Showing error, invalid argument passed – Sonu Oct 30 '18 at 08:11
  • In pluck its getting only first single value, but not getting all...thank you ' – Sonu Oct 30 '18 at 08:13
  • What version of laravel are you using? The pluck behaviour you're describing was changed ages ago – apokryfos Oct 30 '18 at 08:14
  • I am using laravel 4.2 version – Sonu Oct 30 '18 at 08:22
  • In laravel 4.2 `lists` achieves the same result that `pluck` achieves in version 5+ so you can use `lists('id')` to get only the ids – apokryfos Oct 30 '18 at 10:34
  • thank you soo much, it worked. using lists(id), i got only id numbers – Sonu Oct 30 '18 at 10:50

4 Answers4

2

Laravel pluck method will select required fields only:

$est_data = Establishments::where('status',0)->where('city','=',$data['location'])->pluck('id');
return $est_data;

As commented by @apokryfos for your laravel version (4.2) lists method should be used, so it is:

$est_data = Establishments::where('status',0)->where('city','=',$data['location'])->lists('id');
return $est_data;
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Sorry, I thought `pluck` is only supported in `QueryBuilder` not `EloquentBuilder`. Just checked, both supports `pluck` – Ijas Ameenudeen Oct 30 '18 at 09:02
  • 1
    As mentioned in the question comments `pluck` was called `lists` in laravel 4.2 and confusingly laravel 4.2 also had a function called `pluck` which is now called `value` so the logic here is sound but the question should have had the correct laravel version mentioned – apokryfos Oct 30 '18 at 12:12
  • Updated answer to include @apokryfos comment about naming. – u_mulder Oct 30 '18 at 12:18
1

You can use the laravel array_flatten() array helper:

The array_flatten function flattens a multi-dimensional array into a single level array:

$array = array('name' => 'Joe', 'languages' => array('PHP', 'Ruby'));

$array = array_flatten($array);

// array('Joe', 'PHP', 'Ruby');

In your case:

$est_data = Establishments::where('status',0)->where('city','=',$data['location'])->pluck('id');
return array_flatten($est_data);
Remul
  • 7,874
  • 1
  • 13
  • 30
  • Thank u, i tried with the same as u told, but Still i am getting the same result as [{"id":43},{"id":71},{"id":41},{"id":39}] – Sonu Oct 30 '18 at 10:23
0
$result = implode(',',$est_data[0]);

This will solve your problem. You should read about implode(convert to string) and explode (string to array). They are really useful php functions.

Reading your comment, your error is coming from the fact that you try to access $set_data and not $set_data[0] where you values are based on the output you provided us.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0

Use the php array_column function, specifying the id:

$est_data = Establishments::where('status',0)->where('city','=',$data['location'])->pluck('id');
return array_column("id", $est_data);
Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29