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

9
  • did you see stackoverflow.com/questions/1319903/…? Commented Oct 30, 2018 at 8:08
  • Welcome. "but getting error" What would that error be? Commented Oct 30, 2018 at 8:10
  • 1
    Possible duplicate of How to Flatten a Multidimensional Array? Commented Oct 30, 2018 at 8:10
  • $est_data = Establishments::where('status',0)->where('city','=',$data['location'])->get(array('id')); return $implode = implode(' ',$est_data); Showing error, invalid argument passed Commented Oct 30, 2018 at 8:11
  • In pluck its getting only first single value, but not getting all...thank you ' Commented Oct 30, 2018 at 8:13

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I thought pluck is only supported in QueryBuilder not EloquentBuilder. Just checked, both supports pluck
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
Updated answer to include @apokryfos comment about naming.
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);

1 Comment

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}]
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.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.