Php 需要帮助在laravel 5.3中从多个数据库表中以单个视图获取表中的数据吗
因为我正在开发larawel 5.3中的一个项目。我在数据库中使用了两个表,第一个是Php 需要帮助在laravel 5.3中从多个数据库表中以单个视图获取表中的数据吗,php,laravel-5.3,Php,Laravel 5.3,因为我正在开发larawel 5.3中的一个项目。我在数据库中使用了两个表,第一个是usersans,第二个是points您可以在这个链接中看到我的points表结构。 所以当横切发生时。它保存在积分表中,并与用户\u id关联,因此可以将用户的净积分视为使用以下查询 $points = Points::select(DB::raw("sum(p_add)-sum(p_less) as Total"))->where('user_id', Auth::user()->id)->
users
ans,第二个是points
您可以在这个链接中看到我的points表结构。
所以当横切发生时。它保存在积分表中,并与用户\u 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>
身份证件
用户名
电子邮件
国家
日期
要点
{{$u->id}
{{$u->user_name}
{{$u->email}
{{$u->country}
{{$u->created_at}
????
视图上的结果如图所示
现在我不知道如何从
points
表中为每个用户获取净点数。请为我提供一些帮助来解决这个问题。您可以公开函数并在foreach循环中调用它的每个循环。或者在Points模型类上创建静态函数,通过使用user_id作为参数为每个用户获取点。
将getUserPoints
放在Points
model类上
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->id)}}
调用每个循环。您可以在foreach循环中创建公共函数并调用它。或者在Points模型类上创建静态函数,通过使用user_id作为参数为每个用户获取点。
将getUserPoints
放在Points
model类上
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->id)}
您需要一次为一个用户获取所有的点,或者单个点??是的。as--aspooints.sum(p_add)-points.sum(p_less),其中user_id=x
位于每个用户的详细信息前面..不需要使用wher函数来获取您从所有用户的角度处理的点..那么确切的查询是什么呢。用anser告诉我,而不是评论。你需要一次为一个用户获得所有的分数,或者单独获得分数??是的。as--aspooints.sum(p_add)-points.sum(p_less),其中user_id=x
位于每个用户的详细信息前面..不需要使用wher函数来获取您从所有用户的角度处理的点..那么确切的查询是什么呢。用安瑟的口吻告诉我,而不是评论。你是格雷特·阿卜杜勒卡里姆·穆罕默德。。。谢谢你解决了我的问题。再次感谢。。还有一件事我可以在控制器中使用这个函数吗。。??因此,如果您在全局范围中定义它,我可以访问is就像{{getUserPoints($u->id)}
一样,而不是{{\App\Points::getUserPoints($u->id)}
。您可以在app/Helpers
dir中创建名为examplepublic\u functions.php
的文件,并在其上定义重复调用的函数,您必须将其放入composer.json中,以便加载每个请求,使其像laravel中的helper函数一样位于全局范围内<代码>“自动加载”:{“文件”:[“app/Helpers/public_functions.php”,],您是greate Abdulkareem Mohammed…感谢您解决了我的问题。再次感谢..还有一件事我可以在controller中使用此函数..这样我就可以访问的是{getUserPoints($u->id)}
,而不是{{\App\Points::getUserPoints($u->id)}
如果您在全局范围内定义它。您可以在app/Helpers
dir中创建名为examplepublic\u functions.php的文件,并在其上定义重复调用的函数,您必须将其放入composer.json中,以便加载每个请求,使其像laravel中的helper函数一样位于全局范围内。“autoload”:{“文件”:[“app/Helpers/public_functions.php”,],