1
@foreach($users as $user)

$users[] = $user->name; 

@endforeach

I want to get the output like this.

['X', 'Y', 'Z', 'C'];

Note: i need this because i have to use this in JavaScript function.

6
  • 2
    "i need this because i have to use this in JavaScript function" - then use json_encode. Commented Jun 20, 2017 at 7:35
  • Can you show how you intend to use it with javascript? Also, I'm assuming $users is an Eloquent Collection? Commented Jun 20, 2017 at 7:38
  • Thank you for the comment.How can i get rid of these quotes in js now. ["A","B","] @CBroe Commented Jun 20, 2017 at 7:38
  • What version of Laravel are you using? Commented Jun 20, 2017 at 7:40
  • Laravel 5.2 @RossWilson Commented Jun 20, 2017 at 7:40

3 Answers 3

1

First of all, you can't "turn a foreach loop into an array", you would use that foreach loop to get an array as outcome. In this case I wouldn't go with a foreach approach, there are some methods that can help you cleaning out your code a bit.

You have two options depending on the type of the $users variable, pluck() or array_column():

// $users is a collection
json_encode($users->pluck('name'));

// $users is an array
json_encode(array_column($users, 'name'));  

The json_encode() is highly recommended (as the comments pointed out) if you are going to use that output in your javascript. Now you can just send it in a respond and use it as a normal JSON.

In case you print the resulting variable using Blade, remember you need to use {!! !!}, otherwise, if you use {{ }} you would get unwanted scaped characters, since it uses htmlspecialchars() function under the hood.

Hope this helps you.

Sign up to request clarification or add additional context in comments.

4 Comments

I am able to convert it into json,but i am getting this output ["A","B",&quot) instead of ['A','B']
I presume you are using blade to print the json, just use {!! !!} instead of {{ }} to print the variable.
@KhiradBanu Glad it helped you!
@KhiradBanu If it solved your problem, please consider accepting the question so other people can find it.
0

I recommend You should perform this action in your controller only, and then pass it to your view as

$userNames=implode(",",array_column($users,'name')); 
return view('your_view',['userNames'=>$userNames]);

Then split it in your js as

var names="{!! $userNames!!}";
var names= names.split(",");

1 Comment

Why use <?php echo when he states that he is using blade and laravel?
0

Collections will automatically be converted to json when you try to echo them.

The reason you're seeing &quot; is because using {{ }} will automatically escape strings for your protection. You can prevent this by using {!! !!} instead.

Try something like this:

<script>

    var data = '{!! $users->pluck('name') !!}';

    console.log( JSON.parse(data));
</script>

Hope this helps!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.