[英]List all the users with the count of drivers they registered
i have tow models User
and Driver
the user
can register any number of drivers drivers
我有两种型号的
User
和Driver
, user
可以注册任意数量的驱动drivers
in drivers table i have agent_id
, i want to list all the users
with the count of the drivers they registered 在驱动程序表中,我具有
agent_id
,我想列出所有users
以及他们注册的驱动程序数量
in my Usercontroller i want to get the count of the drivers registered by the agents 在我的Usercontroller中,我想获取代理注册的驱动程序数
public function index(Request $request)
{
$users = User::all();
$registration_centers = RegistrationCenter::all();
$drivers_registered = Driver::where('agent_id' , $agent_id)->count(); //something like this but i don't have agent_id to query with
return view('agents', compact('users', 'registration_centers', drivers_registered));
}
Drver Model Drver模型
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Driver extends Model
{
use SoftDeletes;
protected $fillable = ['agent_id','registration_center','event_name','registration_id','profile_photo','first_name','last_name',];
}
and User Model 和用户模型
<?php
namespace App;
use App\Driver;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use SoftDeletes, HasApiTokens, Notifiable;
protected $fillable = ['name', 'email', 'phone_number','password','address_city_village','address_state','image','registration_center','status'];
protected $hidden = ['password', 'remember_token','created_at','updated_at','deleted_at'];
protected $dates = ['deleted_at'];
}
i wan to disply all the users
in a list view with the drivers count of each users 我想在列表视图中显示所有
users
,并列出每个用户的驱动程序数
<ul>
@foreach (users as user)
<li>User: {{user->name}}
Total Drivers: {{$user->drivers_count}}
</li>
@endforeach
</ul>
thank you 谢谢
Example AgentController: 示例AgentController:
public function index(Request $request, $agenId){
$users = User::all();
$registration_centers = RegistrationCenter::all();
$drivers_registered = Driver::where('agent_id' , $agenId)->count();
return view('agents', compact('users', 'registration_centers',
'drivers_registered'));
}
this blade
<p> {{ $drivers_registered }} </p>
Assuming the agent_id
is the key that relates the User to the Driver... you're pretty close to what I think you're asking for. 假设
agent_id
是将用户与驱动程序联系起来的关键...您已经很接近我认为的要求了。
However, in the first line of the index method of your UserController, I would update 但是,在UserController的index方法的第一行中,我将更新
$users = User::all();
to 至
$users = User::withCount('drivers')->all();
and add the drivers relationship to the User model. 并将驱动程序关系添加到用户模型。
So the user model with the relationship added will now be 因此,添加了关系的用户模型现在为
class User extends Authenticatable
{
use SoftDeletes, HasApiTokens, Notifiable;
protected $fillable = ['name', 'email', 'phone_number','password','address_city_village','address_state','image','registration_center','status'];
protected $hidden = ['password', 'remember_token','created_at','updated_at','deleted_at'];
protected $dates = ['deleted_at'];
public function drivers(){
$this->hasMany('App\Driver', 'agent_id');
}
}
You may be able to remove the line below from your controller but that's up to you... 您可能可以从控制器中删除以下行,但这取决于您...
$drivers_registered = Driver::where('agent_id' , $agent_id)->count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.