Sonu
Sonu

Reputation: 301

Convert multiple array into a string in laravel

$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

Upvotes: 0

Views: 794

Answers (4)

u_mulder
u_mulder

Reputation: 54796

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;

Upvotes: 2

Elisha Senoo
Elisha Senoo

Reputation: 3594

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

Upvotes: 0

Remul
Remul

Reputation: 8252

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

Upvotes: 1

pr1nc3
pr1nc3

Reputation: 8348

$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.

Upvotes: 0

Related Questions