4

I want to get a total user transaction (specific user) with relationship. I've done it but i'm curious is my way is good approach.

//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();

What make me curious is part inside countTransaction. I put where where('user_id', Request::get('user_id')) directly inside the model.

is it good approach or any other way to get specific way?

expected result:

"merchant:"{
    "name": "example"
    "username" : "example"
    "transactions": {
        "count_transactions: "4" //4 came from a specific user.
    }
}

I need to get the merchant data with the transaction count for specific user. This query is based on logged in user. so when a user access merchant page, they can see their transaction count for that merchant.

Thanks.

9
  • Is Merchant your user? Or do you have an actual User model? Commented Jan 4, 2016 at 5:11
  • I have an actual user. 3 table. users, merchants, and transactions Commented Jan 4, 2016 at 5:12
  • So a User also hasMany transactions? Commented Jan 4, 2016 at 5:13
  • Yes. User has many transactions Commented Jan 4, 2016 at 5:14
  • And do you simply want to count the # of transactions for a specific user? I'm trying to understand how Merchant plays into this. Commented Jan 4, 2016 at 5:15

1 Answer 1

1

You really want to keep request data outside of your models (instead opting to pass it in). I'm also a little confused about why you have both a 'hasOne' for transactions, and a 'hasMany' for transactions within the merchant model.

I would probably approach the problem more like the below (untested, but along these lines). Again I'm not fully sure I understand what you need, but along these lines

    // 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); 
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, thanks for trying to understand my question. The reason i make 2 transactions is: countTransaction will return one value not array. that's why i make it hasOne instead of using hasMany. I've tried your solution using $singleTotal it's great but I can't get merchant result. Thanks
Could you post the result you did get?
result: [10]. I've update my question hope can be more understandable. This query is based on logged in user. so when a user access merchant page, they can see their transaction count for that merchant.
What is the output of the first command (the map one)? The intended output of the second, would be a single number (in this case 10)
[ { "id": 1, "name": "merchant 1", "transactions": [10] } { "id": 2, "name": "Merchant 2", "transactions": [3] } ]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.