8

So I'm trying to convert a Laravel array into json so I can then manipulate it through javascript. Im not sure how this is achieved correctly. Here is the code, so far

@foreach ($posts as $post)
<div class="row">
   <div class="col-md-8">
     <div class="row">
        <div class="col-md-8 tag">
            <h4><strong><a href="{{{ $post>postName }}}">#{{String::title($posts->postName) }}</a></strong></h4>
        </div>
     </div>
    <!-- ./ post title -->
   </div>    
</div>
<hr />
@endforeach


<script type="text/javascript">
   var data = "{{ ($posts) }}"; // ??
   console.log(data);
</script>

5 Answers 5

12

You could return an json_encoded array from the controller like so:

public function index()
{
    $posts = Post::all();
    $json = json_encode($posts);
    return View::make('posts.index', compact('posts', 'json'));
}

Which you then can work on in your view like you'd like:

<script type="text/javascript">
    var data = {{ $json }};
    console.log(data);
</script>

Also, if you have sensitive fields on your post model, you should exclude these in the model class to prevent them to show in your javascript inspector:

class Post extends \Eloquent {
    ...

    protected $hidden = array(
    'id',
    'created_at',
    'updated_at'
    );

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

Comments

8

If you looking for "high-level" way to convert plain array to json, you can use laravel collections.

collect(['a' => 1, 'b' => 2, 'c' => 3])->toJson();

Comments

5

Use Eloquents built in function toJson to get your rows as json.

<script type="text/javascript">
   var data = "{{ $posts->toJson() }}";
   console.log(data);
</script>

If there's some fields you don't want to include, add the field to the hidden property in your model as Jimmy mentioned.

1 Comment

This is cleaner, use this in combination with the hidden field in model.
5
<script type="text/javascript">
   let data = @json($posts);
   console.log(data);
</script>

1 Comment

This should be the correct answer. More info under Rendering JSON : laravel.com/docs/7.x/blade
0

you don't need to do all that, just use the php inbuilt function json_encode

$json = json_encode($posts);
    echo $json;

<script type="text/javascript">
   var data = "$json"; // ??
   console.log(data);
</script>

2 Comments

Hey I tried that but it just prints empty braces. I need the above code since it displays all the post titles but also I then need to do some client side processing with those same posts. The second aspect is what I cant figure out
json_encode will ensure that json data is properly encoded from an array, once encoded you can use json_decode function to generate a json object which can be further used to display specific object values

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.