简体   繁体   English

在laravel 5.3中需要帮助以从多个数据库表中获取单个视图中的表中的数据

[英]Need help to in laravel 5.3 to fetch data in a table in single view from multiple database tables

As I am developing a project in larawel 5.3. 因为我正在开发一个废弃物5.3的项目。 and I am using two tables in database first users ans second points you can see my points table structure in this link. 我在数据库中使用两个表第一个users和第二个points你可以在这个链接中看到我的点表结构。 I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their So when a transection occurs. 我希望在laravel 5.3控制器中使用Auth :: user() - > id获取用户ID但是当发生横切时它会产生错误 it is saved in points table getting assousiated with user_id so net points of a user can can be taken as using folloing query 它被保存在与user_id点表中,因此可以将用户的网点视为使用下面的查询

$points = Points::select(DB::raw("sum(p_add)-sum(p_less) as Total"))->where('user_id', Auth::user()->id)->first();

now I am goin to gett all data of users in a single admin page. 现在我要在一个管理页面中获取用户的所有数据。 so I am using following code in controller 所以我在控制器中使用以下代码

public function users_list()
{
    $ptitle = "Website Users";
    $pdesc = "";

    $total_users = User::count();


    $users = User::select('*')->orderby('created_at', 'desc')->get();

        return view('admin.all-users-list',
                        ['ptitle' => $ptitle,
                        'pdesc' => $pdesc,
                            'users' => $users,
                            'total_users' => $total_users,
                        ]);
}

And folloing foreach loop to fetch users data in view page 并且下载foreach循环以在视图页面中获取用户数据

<table class="table table-hover">
                            <tbody><tr>
                                <th>ID</th>
                                <th>User Name</th>
                                <th>Email</th>
                                <th>Country</th>
                                <th>Date</th>
                                <th>Points</th>
                            </tr>

                            <?php foreach ( $users as $u ){ ?>

                            <tr>
                                <td>{{ $u->id }}</td>
                                <td>{{ $u->user_name }}</td>
                                <td>{{ $u->email }}</td>
                                <td>{{ $u->country }}</td>
                                <td>{{ $u->created_at }}</td>
                                <td>????</td>
                            </tr>



                            <?php }?>

                            </tbody></table>

and result on view is as in this image 视图上的结果与此图像相同 用户表图像

now I dont now that how can I fetch net points from points table for every user. 现在我不知道如何从每个用户的points表中获取净点数。 please provide me some help to solve this issue. 请帮我解决一下这个问题。

you can make public function and call it every loop in foreach loop. 你可以创建公共函数并在foreach循环中的每个循环中调用它。 Or create static function on the Points model class to get points for every user by using user_id as argument. 或者在Points模型类上创建静态函数,以使用user_id作为参数为每个用户获取点。 Put getUserPoints on Points model class getUserPoints放在Points模型类上

public static function getUserPoints($userId){
    $points = self::select(DB::raw("sum(p_add)-sum(p_less) as Total"))
                      ->where('user_id', $userId)->first();
    if( $points != null ){
       return $points->Total;
    }
       return 0;

    }
}

And you can call it every loop as {{ \\App\\Points::getUserPoints($u->id) }} 您可以将每个循环称为{{ \\App\\Points::getUserPoints($u->id) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM