Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to count column from database but when I get data from database with controller, count is returning just 0. It should be more than. I don't know where is the problem. I'm beginner Laravel by the way. So, how can I count data from database?

My controller:

public function getProfile($username){
    $follow = Follow::all();
    $user = User::where('username', $username)->first();
    if(isset($user)) {
        return view('design2.profile', ['user'=>$user,'follow'=>$follow]);        
    } else {
        return redirect()->route('home');
    }
}

My blade is:

@foreach($user->follows as $followr)  
    <h6 >
        <strong>
            {{ count((array)$followr->following_id) }}
        </strong>
    </h6>
@endforeach
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

Since you are iterating a collection in the foreach of blade, you can call the count() method of the collections.

The count method returns the total number of items in the collection:

$collection = collect([1, 2, 3, 4]);

$collection->count();

// 4

So in your code it would only be.

$user->follows->count()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...