2

in controller I do this

$actions = array('' => 'Select Action Name') + Action::lists('name' , 'id');

I want to use that $actions variable in my javascript.

I tried these ways:

<script>
var actions = {{actions}};
</script>

but I got this excpetion

Array to string conversion

I also tried this:

<script>
    @foreach($actions as $action)
        console.log($action);
    @endforeach
</script>

but in the console I got this exception

Uncaught ReferenceError: $action is not defined 

could you help me please?

5
  • I wonder why you downvote the question. I tried hard to solve the problem and I showed you a good efforts. Commented Jul 21, 2014 at 17:11
  • 3
    +1 @AnastasieLaurent is right. Failing at your attempts is kind of the reason for posting a question. I think she's shown real effort here. Commented Jul 21, 2014 at 17:12
  • There is a nice package to help you pass easily data from Laravel to js here. Have a look it deserves the effort. You can pass data at once by doing: JavaScript::put(['foo' => 'bar']); Now var foo = 'bar'; is accessible to the window scope for your js inside script tags. A really clean solution. Commented Jul 21, 2014 at 17:27
  • @marios WOW fascinated ... Absolutely I will use it. Many Thanks Commented Jul 21, 2014 at 17:33
  • Thanks keep on the good job! Commented Jul 21, 2014 at 17:49

2 Answers 2

3

You have to echo it with curly braces:

<script>
    @foreach($actions as $id => $name)
        console.log('{{ $id }}', '{{{ $name }}}');
    @endforeach
</script>

Your other option is to use the array by converting it to JSON:

<script>
    var actions = {{ json_encode($actions) }};
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks I got the result but for some reasons the result are not correct. each $action is a name and id, what I got is just the name . how can I take both of them please? +1
@AnastasieLaurent - The second example I've shown you should give you what you want.
I edited some of your code so the final code is this <script> var actions = {{ json_encode($actions) }}; for (var key in actions){ console.log(key, actions[key]); } </script>`` and it works :) many thanks I will accept the answer when the system allows
1

$actions is an array inside of your controller. To pass it down to a view, you could do this inside of a route callback of a controller:

return View::make('someview')->with('actions', $actions);

Then you would be able to access the $actions variable, an array, from within your view.

However, there are some big differences between Javascript Arrays and PHP arrays. Most likely, when you convert a PHP array to JavaScript you wind up with a JavaScript object instead, since arrays in javascript cannot have non-numeric indices.

What you probably want to do is json_encode your array in PHP, and then you can JSON.parse inside your JavaScript:

<script>
    var actions = {{ json_encode($actions) }};
    console.log(actions);
</script>

5 Comments

+1 to your efforts, but the other answer helped me first. Many thanks
You can only call JSON.parse on a string. Drop it since it doesn't work and there's no need for it.
From the manual, this is the function description: string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) If you don't want a string in the JavaScript variable, how do you get the data?
@watcher It returns a string, which you are then echoing directly into the page. That string is then parsed in JS - not as a string - as regular JS code, so it ends up being an object automatically. Try it and you'll see what I mean.
@Joseph Silber gotcha, makes sense now.

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.