-1

I have this object and as you can see I sort by different variables

$banks = Bank::with('credits')->with('deposits')->with('bank_assets')->with('atms')->get()->sortByDesc($sort);

So I have relations in my model for example bank_assets (credits and deposits working clearly and i can sort by deposit or by credit) But bank_assets! No In this case I need to sort by actives which I need to calculate. Because Actives this is a summ of different ammounts from this table http://prntscr.com/ij3idg And In my model relation I get only the object not a summ of this amounts As you can see

//Bank Model
    public function bank_assets()
    {

        return $this->hasMany('App\BankRating')->where('type','assets');
    }

But I need to sort by actives which is a summ of different amounts Please Help me and Explaine How I can sort

David
  • 181
  • 1
  • 2
  • 8

1 Answers1

0

Try this, this would sort according to $sort parameter and ammount property on bank_assets relation:

$banks = Bank::with([
    'credits',
    'deposits',
    'bank_assets'
    'atms',
])->get()->sortByDesc(function($bank) use ($sort) {
    if($sort == 'actives') {
        return $bank->bank_assets->sum('ammount');
    }
    return $bank->$sort;
});

hope this works. ;)

Ruman
  • 191
  • 1
  • 8
  • And if I dont know which ellement I need to summ and the quantaty of them Just condition -> all ammounts which connected with this bank_id and has type assets – David Feb 24 '18 at 12:36
  • what do you mean by `all ammouts` – Ruman Feb 24 '18 at 12:39
  • I dont understand what do you mean field1+field in my case what is field – David Feb 24 '18 at 12:46
  • If I type such $banks = Bank::with([ 'credits', 'deposits', 'bank_assets' => function ($query) { $query->selectRaw('*, amount as actives') ->orderBy('actives', 'desc'); }, 'atms', ])->get()->sortByDesc($sort); – David Feb 24 '18 at 12:47
  • `field1` can be the names of the columns on basis of whose sum, you are going to sort. for example you want to sort according to the sum of `amount` and `tax` columns then your fields you should use `selectRaw('*, amount+tax as actives')`. And for the error that you get, make sure you have `bank_assets` method on your `Bank` Model. – Ruman Feb 24 '18 at 12:56
  • No I want just to sort by ammount field, Please Help me to make query – David Feb 24 '18 at 12:58
  • I am trying to make something like this – David Feb 24 '18 at 13:03
  • `switch($sort){ case "actives" : $banks = Bank::with('credits')->with('deposits')->with('bank_assets')->with('atms')->get(); foreach ($banks as $bank){ foreach ($bank->bank_assets as $asset){ $bank->actives=$bank->actives+$asset->amount; } } usort($banks,[$this,'mySort']); default: $banks = Bank::with('credits')->with('deposits')->with('bank_assets')->with('atms')->get()->sortByDesc($sort); }` But here I can not use usort because of the object not array – David Feb 24 '18 at 13:04
  • And how I can use $sort variable which coming from the GET parametr? – David Feb 24 '18 at 13:11
  • I need something like this `$banks = Bank::with([ 'credits', 'deposits', 'bank_assets', 'atms', ])->get()->sortByDesc($sort,(function($bank) { if($sort==actives){ return $bank->bank_assets->amount; } })); `But it is not correct – David Feb 24 '18 at 13:14
  • what is that GET parameter for? Do you want sort acccording to parameter or `ammount`? Please explain more in your question. Chck my answer again. – Ruman Feb 24 '18 at 13:16
  • Or Something Like This http://prntscr.com/ij4ffm But it didnt sort by actives (( http://prntscr.com/ij4h5b – David Feb 24 '18 at 13:30
  • Answer Updated! – Ruman Feb 24 '18 at 13:36
  • credits and deposits should be working fine if these properties exist on `bank` model and passed into $sort as GET parameter. – Ruman Feb 24 '18 at 13:40
  • Ah! Fixed that. – Ruman Feb 24 '18 at 13:41
  • You should slect this answer if it solved your problem. – Ruman Feb 24 '18 at 14:09