kaiden's avatar

scan through json data in blade

i am getting this or similar JSON data in the view.

[{"follow_id":1,"follower":"kai","following":"birkan","created_at":"2018-06-17 19:05:22"},{"follow_id":2,"follower":"none","following":"birkan","created_at":"2018-06-17 19:10:27"}]

all i want is to look if any "follower" property has the name of the urrent user, like

if( one_of_the_follower == Auth::user()->name )
              .......

how i can scan this data in that way? Thanks.

0 likes
7 replies
Yorki's avatar

Consider doing such logic in Controller

$currentUserIsFollower = false;

foreach ($jsonData as $data) {
    if ($data['follower'] === Auth::user()->name) {
        $currentUserIsFollower = true;
    }
}

return view('some-view', [
    'isFollower' => $currentUserIsFollower,
]);

some-view.blade.php

@if ($isFollower)
    I'm follower
@endif 
kaiden's avatar

@jlrdw its strange i was thinking to write your name after snapey and cronix. you know you are the best in here.

i know its not paid but i saw @cronix answered another questions within last few minutes so i thought while hes on other questions maybe he can take a look at my question too.

of course it not appropiate to do so. but your answers on point most of the time and i really tired of searching answers on google.

whatever....

the main problem is this thing.

whats that?

[{"follow_id":1,"follower":"kai","following":"birkan","created_at":"2018-06-17 19:05:22"},{"follow_id":2,"follower":"none","following":"birkan","created_at":"2018-06-17 19:10:27"}]

Is it an PHP array or its a JSON object. i am tangled. Is there a function which can look for every "follower" property and tell me at least one of them consist "kai" or not thats the real question in here

jlrdw's avatar
jlrdw
Best Answer
Level 75

An example I saved a while back:

    public function testJson_bak()
    {
        $data = '[{  
    "ClientID":24,
    "Name":"Client1",
    "Balance1":null,
    "Balance2":null
},
{  
    "ClientID":25,
    "Name":"Client2",
    "Balance1":24,
    "Balance2":0
}]';
        //dd($data);
        $decoded = json_decode($data, true);
        //dd($decoded);
        foreach ($decoded as $d) {
            foreach ($d as $k => $v) {
                echo "$k - $v\n";
            }
        }
    }

Perhaps that will get you going, a foreach with an if to find needed value should work.

1 like
Cronix's avatar
[{"follow_id":1,"follower":"kai","following":"birkan","created_at":"2018-06-17 19:05:22"},{"follow_id":2,"follower":"none","following":"birkan","created_at":"2018-06-17 19:10:27"}]

It's a javascript array, of 2 json objects. Where does it come from? Is it just loaded in the controller an passed to the view, or is this the result of an ajax request?

kaiden's avatar

@cronix loaded in the controller and passed to the view. Its like that because its loads a "hasMany" relationship. like......

$entries = Entry::where('topic_fk', $topic)
           ->with('user.following')  //thats where its coming from
           ->orderBy('created_at', 'desc')
           ->paginate(10);

Please or to participate in this conversation.