I now have two tables:
One is the association list (list) and the user table (user)
List table fields
list_id(協(xié)會(huì)id) , list_name(協(xié)會(huì)名稱)
user user table
user_id(用戶id),list_id(所屬協(xié)會(huì)), username(用戶名)
How to achieve this:
Related query to obtain the total number of members under each association
For example:
{"list_name":"AAA協(xié)會(huì)","user_count":20},
{"list_name":"BBB協(xié)會(huì)","user_count":211}
...
How to connect?
擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...
class List extends Model
{
protected $fillable = ['name'];
public function users()
{
return $this->hasMany(User::class, 'list_id', 'id');
}
}
class User extends Model
{
protected $fillable = ['list_id'];
public function list()
{
return $this->belongsTo(List::class, "list_id", "id");
}
}
List::withCount("users")->paginate();
Read more documentation.