0

In laravel I am trying to pass json data into my profile.blade.php view. I have the json query in my apicontroller.php which pulls data from my profile db table and then passes the data into the profile.blade.php using the profilecontroller.js.

I can see the data as an array in profile.blade.php using @{{ profiles }} I get the output

[{"id":1,"username":"test","email":"[email protected]","password":"$2y$10$q2N/p3AD8FfUHxRVOF.aoecIKy8qcOBgvVurP4tZrcRdvZSS3JDOK","avatar":"default.jpg","remember_token":null,"created_at":"2016-12-07 22:29:57","updated_at":"2016-12-07 22:29:57"}]

but if i try to get a single object e.g {{ profiles.username }} it doesn't output anything

How can i pull just the username or another object out of my array?

apiController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\UserDetail;
use App\Users;
use View;
use Auth;

class ApiController extends Controller
{
public function showAll()
{
    return response()->json(
    $details = Users::get()
    );

    }
}

profilecontroller.js

app.controller('profileController', function($scope, $http) {

$http.get('/api/hello')
.success(function(data, status, headers, config) {
 $scope.profiles = data;
})
.error(function(error, status, headers, config) {
 console.log(status);
 console.log("Error occured");
});

profile.blade.php

@ {{profile}} 
3
  • What javascript framework are you using to do this? And can you post the entire view you are using? Commented Dec 8, 2016 at 9:58
  • 3
    You have your object in an array. What you should do is: {{ profiles[0].username }} Commented Dec 8, 2016 at 9:58
  • Thanks works perfectly, forgot to mention this is using angular Commented Dec 8, 2016 at 10:02

1 Answer 1

1

Your object is within an array so you must indicate which array entry your code is referring to, even if there is only one entry in the array.

Arrays are zero indexed, so the first item in an array will always have the index [0]

thus

@{{ profiles[0].username }}

Should achieve what you're trying to do.

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

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.