【问题标题】:Laravel relationship count()Laravel 关系计数()
【发布时间】:2016-04-07 16:37:21
【问题描述】:

我想获得一个有关系的总用户交易(特定用户)。 我已经做到了,但我很好奇我的方法是好方法。

//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 访问商家页面时,他们可以看到该商家的交易计数。

谢谢。

【问题讨论】:

  • Merchant 是您的用户吗?或者你有实际的User 模型吗?
  • 我有一个实际用户。 3 表。用户、商家和交易
  • 所以User 也有hasMany 交易?
  • 是的。用户有很多交易
  • 您是否只想计算特定用户的交易次数?我试图了解Merchant 是如何参与其中的。

标签: php laravel eloquent relationship


【解决方案1】:

您确实希望将请求数据保留在模型之外(而不是选择传入)。我也有点困惑,为什么在商家模型中你既有用于交易的“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); 

【讨论】:

  • 您好,感谢您尝试理解我的问题。我进行 2 笔交易的原因是:countTransaction 将返回一个值而不是数组。这就是为什么我使用hasOne 而不是使用hasMany。我已经使用$singleTotal 尝试了您的解决方案,这很棒,但我无法获得商家结果。谢谢
  • 你能发布你得到的结果吗?
  • 结果:[10]。我已经更新了我的问题,希望可以更容易理解。 This query is based on logged in user. so when a user access merchant page, they can see their transaction count for that merchant.
  • 第一个命令(地图命令)的输出是什么?第二个的预期输出将是一个数字(在本例中为 10)
  • [ { "id": 1, "name": "merchant 1", "transactions": [10] } { "id": 2, "name": "Merchant 2", "transactions": [3] } ]
猜你喜欢
  • 2014-08-04
  • 1970-01-01
  • 2020-06-22
  • 2019-07-18
  • 2021-12-27
  • 2019-05-16
  • 2016-03-09
  • 2017-05-01
  • 1970-01-01
相关资源
最近更新 更多