Laravel - 根据特定条件从数据库中获取用户数据
Laravel - getting data of the user from database by specific criterion
我有一个用户table,它存储我的用户和应用程序的名称table,它存储用户所做的所有应用程序...用户提出运输申请并打印该应用程序在管理员页面上,以便管理员可以知道哪个用户提出了申请。
如何检索申请用户的姓名?我如何获得所有申请面包车的用户?
一个用户有多个管理员如下:
用户模型:
public function admin(){
return $this->hasMany('Martin\Models\admin');
}
管理员模型:
public function application()
{
return hasMany('Martin\Models\Application');
}
应用程序table架构:
Schema::create('applications', function (Blueprint $table)
{
$table->increments('id');
$table->string('user_id')->nullable();
$table->string('destination');
$table->date('departure_date');
$table->integer('Number_of_days_hired');
$table->timestamp('departure_time')->nullable();
$table->boolean('approved')->default(0);
$table->timestamps();
});
$applications = Application::all(); //get all applications. Application is your Eloquent model of the Applications table
$userIds = $applications->pluck('user_id'); // get ids from all the users that applied
$names = User::whereIn($userIds)->pluck('name'); // name is equal to the name of the column in database which stores name of the User (you didn't specify that in your question)
foreach($names as $name) {
// do something with name
}
更新
有两种方法可以找到申请特定事物的用户:
1. blade 文件:
@foreach($users as $user)
@if($user->applied_for_van) // put your criteria for van application here
<td>{{$user->name}}</td>
@endif
@endforeach
2.1 获取所有用户的文件:
$users_who_applied_for_van = [];
foreach($users as $user) {
if($user->applied_for_van) {
$users_who_applied_for_van[] = $user; // this equal to array_push. add user to the array basically
}
}
2.2 blade 文件:
@foreach($users_who_applied_for_van as $user)
<td>{{$user->name}}</td>
@endforeach
我有一个用户table,它存储我的用户和应用程序的名称table,它存储用户所做的所有应用程序...用户提出运输申请并打印该应用程序在管理员页面上,以便管理员可以知道哪个用户提出了申请。
如何检索申请用户的姓名?我如何获得所有申请面包车的用户?
一个用户有多个管理员如下:
用户模型:
public function admin(){
return $this->hasMany('Martin\Models\admin');
}
管理员模型:
public function application()
{
return hasMany('Martin\Models\Application');
}
应用程序table架构:
Schema::create('applications', function (Blueprint $table)
{
$table->increments('id');
$table->string('user_id')->nullable();
$table->string('destination');
$table->date('departure_date');
$table->integer('Number_of_days_hired');
$table->timestamp('departure_time')->nullable();
$table->boolean('approved')->default(0);
$table->timestamps();
});
$applications = Application::all(); //get all applications. Application is your Eloquent model of the Applications table
$userIds = $applications->pluck('user_id'); // get ids from all the users that applied
$names = User::whereIn($userIds)->pluck('name'); // name is equal to the name of the column in database which stores name of the User (you didn't specify that in your question)
foreach($names as $name) {
// do something with name
}
更新
有两种方法可以找到申请特定事物的用户:
1. blade 文件:
@foreach($users as $user)
@if($user->applied_for_van) // put your criteria for van application here
<td>{{$user->name}}</td>
@endif
@endforeach
2.1 获取所有用户的文件:
$users_who_applied_for_van = [];
foreach($users as $user) {
if($user->applied_for_van) {
$users_who_applied_for_van[] = $user; // this equal to array_push. add user to the array basically
}
}
2.2 blade 文件:
@foreach($users_who_applied_for_van as $user)
<td>{{$user->name}}</td>
@endforeach