tom harrison
tom harrison

Reputation: 3423

Outputting single json object from array

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}} 

Upvotes: 0

Views: 43

Answers (1)

Joe
Joe

Reputation: 4738

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.

Upvotes: 1

Related Questions