Laravel 关系计数()
Laravel relationship count()
我想获得具有关系的总用户交易(特定用户)。
我已经做到了,但我很好奇我的方法是不是很好。
//User Model
public function Transaction()
{
return $this->hasMany(Transaction::class);
}
//Merchant Model
public function Transaction()
{
return $this->hasMany(Transaction::class);
}
public function countTransaction()
{
return $this->hasOne(Transaction::class)
->where('user_id', Request::get('user_id'))
->groupBy('merchant_id');
}
public function getCountTransactionAttribute()
{
if ($this->relationLoaded('countTransaction'))
$this->load('countTransaction');
$related = $this->getRelation('countTransaction');
return ($related) ? (int)$related->total_transaction : 0;
}
//controller
$merchant = Merchant::with('countTransaction')->get();
让我好奇的是里面的部分countTransaction
。我把 where where('user_id', Request::get('user_id'))
直接放在模型里面。
这是好的方法还是其他方法来获得特定的方法?
预期结果:
"merchant:"{
"name": "example"
"username" : "example"
"transactions": {
"count_transactions: "4" //4 came from a specific user.
}
}
我需要获取商家数据以及特定用户的交易次数。此查询基于登录用户。因此,当 user
访问商家页面时,他们可以看到该商家的交易次数。
谢谢。
您确实希望将请求数据保留在模型之外(而不是选择将其传入)。我也有点困惑,为什么你有一个 'hasOne' 用于交易,还有一个 'hasMany' 用于商家模型中的交易。
我可能会更像下面那样处理问题(未经测试,但按照这些思路)。同样,我不确定我是否理解您的需求,但按照这些思路
// Merchant Model
public function transactions()
{
return $this->hasMany(Transaction::class);
}
public function countTransactionsByUser($userId)
{
return $this
->transactions()
->where('user_id', $userId)
->get()
->pluck('total_transaction')
->sum();
}
// Controller
$userId = request()->get('user_id');
// ::all() or however you want to reduce
// down the Merchant collection
//
$merchants = Merchant::all()->map(function($item, $key) {
$_item = $item->getAttributes();
$_item['transactions'] = [
'count_transactions' => $item->countTransactionsByUser($userId);
];
return $_item;
});
// Single total
// Find merchant 2, and then get the total transactions
// for user 2
//
$singleTotal = Merchant::find(2)
->countTransactionsByUser($userId);
我想获得具有关系的总用户交易(特定用户)。 我已经做到了,但我很好奇我的方法是不是很好。
//User Model
public function Transaction()
{
return $this->hasMany(Transaction::class);
}
//Merchant Model
public function Transaction()
{
return $this->hasMany(Transaction::class);
}
public function countTransaction()
{
return $this->hasOne(Transaction::class)
->where('user_id', Request::get('user_id'))
->groupBy('merchant_id');
}
public function getCountTransactionAttribute()
{
if ($this->relationLoaded('countTransaction'))
$this->load('countTransaction');
$related = $this->getRelation('countTransaction');
return ($related) ? (int)$related->total_transaction : 0;
}
//controller
$merchant = Merchant::with('countTransaction')->get();
让我好奇的是里面的部分countTransaction
。我把 where where('user_id', Request::get('user_id'))
直接放在模型里面。
这是好的方法还是其他方法来获得特定的方法?
预期结果:
"merchant:"{
"name": "example"
"username" : "example"
"transactions": {
"count_transactions: "4" //4 came from a specific user.
}
}
我需要获取商家数据以及特定用户的交易次数。此查询基于登录用户。因此,当 user
访问商家页面时,他们可以看到该商家的交易次数。
谢谢。
您确实希望将请求数据保留在模型之外(而不是选择将其传入)。我也有点困惑,为什么你有一个 'hasOne' 用于交易,还有一个 'hasMany' 用于商家模型中的交易。
我可能会更像下面那样处理问题(未经测试,但按照这些思路)。同样,我不确定我是否理解您的需求,但按照这些思路
// Merchant Model
public function transactions()
{
return $this->hasMany(Transaction::class);
}
public function countTransactionsByUser($userId)
{
return $this
->transactions()
->where('user_id', $userId)
->get()
->pluck('total_transaction')
->sum();
}
// Controller
$userId = request()->get('user_id');
// ::all() or however you want to reduce
// down the Merchant collection
//
$merchants = Merchant::all()->map(function($item, $key) {
$_item = $item->getAttributes();
$_item['transactions'] = [
'count_transactions' => $item->countTransactionsByUser($userId);
];
return $_item;
});
// Single total
// Find merchant 2, and then get the total transactions
// for user 2
//
$singleTotal = Merchant::find(2)
->countTransactionsByUser($userId);