【问题标题】:Need help to in laravel 5.3 to fetch data in a table in single view from multiple database tables在 laravel 5.3 中需要帮助以从多个数据库表的单个视图中获取表中的数据
【发布时间】:2017-02-20 05:18:55
【问题描述】:

当我在 larawel 5.3 中开发一个项目时。我首先在数据库中使用两个表users 和第二个points 你可以在这个链接中看到我的积分表结构。 I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their 所以当横断发生时。它保存在与user_id 关联的积分表中,因此可以将用户的净积分视为使用以下查询

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

现在我要在一个管理页面中获取用户的所有数据。所以我在控制器中使用以下代码

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,
                        ]);
}

并按照 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>

查看结果如下图

现在我不知道如何从points 表中为每个用户获取净点数。请帮我解决这个问题。

【问题讨论】:

  • 您需要为用户一次获得所有积分,或者单独获得积分??
  • 是的。 as -- as poiunts.sum(p_add)-points.sum(p_less) where user_id = x 在每个用户的详细信息前面..
  • 当您处理所有用户的观点时,无需使用 wher 函数来获取点...
  • 那么确切的查询是什么。告诉我,而不是评论。

标签: php laravel-5.3


【解决方案1】:

您可以创建公共函数并在 foreach 循环中的每个循环中调用它。或者在 Points 模型类上创建静态函数,以使用 user_id 作为参数为每个用户获取积分。 将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;

    }
}

您可以将每个循环都称为{{ \App\Points::getUserPoints($u-&gt;id) }}

【讨论】:

  • 你是伟大的 Abdulkareem Mohammed...感谢您解决了我的问题。再次感谢.. 还有一件事我可以在控制器中使用此功能..??所以我可以访问就像{{ getUserPoints($u-&gt;id) }} 而不是{{ \App\Points::getUserPoints($u-&gt;id) }}
  • 如果你在全局范围内定义它。您可以在 app/ Helpers 目录中创建名为示例 ​​public_functions.php 的文件,并在其上定义重复调用的函数,并且您必须将其放在 composer.json 中以加载每个请求以在全局范围内,如 laravel 中的辅助函数. "autoload": { "files" : [ "app/Helpers/public_functions.php", ],
猜你喜欢
  • 2012-03-05
  • 2021-12-24
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2019-10-24
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多