i have tow models User
and Driver
the user
can register any number of drivers drivers
in drivers table i have agent_id
, i want to list all the users
with the count of the drivers they registered
in my Usercontroller i want to get the count of the drivers registered by the agents
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
<?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
<ul>
@foreach (users as user)
<li>User: {{user->name}}
Total Drivers: {{$user->drivers_count}}
</li>
@endforeach
</ul>
thank you
Example 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.
However, in the first line of the index method of your UserController, I would update
$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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.