I have 3 table 'Admins', 'Agent', 'Books'
Admin can add multiple Agents, Agents can add multiple books.
Now I need to count the book for a single Admin. (Every table has the proper foreign key).
How can I achieve this?
My code
//In Controller
$data = Admin::latest()->withCount('count')->get();
//In Admin.php
public function count(){
return $this->hasMany(Agent::class, 'adminId', 'id');
}
//In Agents.php
public function count(){
return $this->hasMany(Books::class, 'AgentId', '???');
}
//In Book.php
public function count(){
return $this->belongsTo(???::class, ???);
}
??? - I don't know which Id or class need to pass.
Can anyone help to solve this issue, please?