2

I have the relations user->sales->detail_sales, a user has one or many sales, a sale has one or more details. What I'm trying to do is obtain with ELOQUENT, the total of all sales per user, that is, make a sum(totaldetails) of the calculated total with withCount.

Sale.php

public function detail()
{
    return $this->hasMany(Detail:class);
}


public function user()
{
    return $this->belongsTo('App\User');
}

DetailSale

public function sale()
{
    return $this->belongsTo(Sale::class);
}

User

public function sales()
{
    return $this->hasMany(Sale::class );
}

This query I currently have

return App\User::with(['sales' => function($query){
    // get total for detail_sales for sale
    $query->withCount(['detail as totaldetails' => function($que){
        $que->select(DB::raw('sum(subtotal)'));
    }]);
}])->get();

but I want that in the User model, to have a property like salestotal for example, is it possible?

@foreach($users as $user)
  {{ $user->salestotal}}
@endforeach
5
  • Use as like $que->select(DB::raw('sum(subtotal) as salestotal')); Commented May 1, 2019 at 2:03
  • @NikleshRaut What I need is the sum of all the values returned by sum (subtotal) ie sum (totaldetails) and have it as property in the parent model User. Commented May 1, 2019 at 2:06
  • Please post the sales and detail_sales relationships. Commented May 1, 2019 at 2:08
  • @JonasStaudenmeir I have added the relations, I hope you understand what I want to obtain, if not comment and provide necessary information Commented May 1, 2019 at 2:14
  • where does the subtotal column lies, in the sales table or in the details table? could you share the tables structure with us please. Commented May 1, 2019 at 6:37

3 Answers 3

2

Why not just use the sum() function since it's all in a Collection?

@foreach($users as $user)
    {{ $user->sales->sum('totaldetails') }}
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

1

You can use withCount() with a HasManyThrough relationship:

class User extends Model
{
    public function details()
    {
        return $this->hasManyThrough(Detail:class, Sale::class);
    }
}

return App\User::withCount(['details as salestotal' => function($query) {
    $query->select(DB::raw('sum(subtotal)'));
}])->get();

@foreach($users as $user)
    {{ $user->salestotal }}
@endforeach

Comments

0

I hope this helps:

    return App\User::with(['sales' => function($query){
     // get total for detail_sales for sale
        $query->withCount(['detail as totaldetails' => function($que){
       $que->select(DB::raw("SUM(subtotal) as salestotal"));
       }]);
    }])->get();

1 Comment

Thank you for responding, but there is no point in copying the code of my question as an answer, I'm not looking for something I already have.

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.