B
B
bpGusar2017-08-08 11:38:46
Laravel
bpGusar, 2017-08-08 11:38:46

How can I make a query so that it would be possible to display the number of rows in a loop?

concocted a request:

$user = DB::table('users')
            ->join('users_group', 'users.id_user_group', '=', 'users_group.id_user_group')
            ->join('posts', 'users.id', '=', 'posts.id_user')
            ->select('users.*', 'users_group.group_name', 'posts.id')
            ->orderby('created_at','asc')
            ->paginate(10);

here the user table is associated with user_group and posts rummaged
through the documentation, did not understand anything, how can I calculate how many posts this or that user has and then display this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kuznetsov, 2017-08-08
@dima9595

Use "relationships" for this. Personally, I use them. And then break into a stake. posts and display what you need

S
shagguboy, 2017-08-08
@shagguboy

add subselect with count

A
Alexander Lysenko, 2017-08-08
@LysenkoSasha

$users = DB::table('users')->count();
https://laravel.com/docs/5.4/queries#Aggregates

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question