【发布时间】: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