dimanche 6 novembre 2016

Laravel eloquent model funtion returns empty

In my project, LedgerModel.php has functions like below:

public function entries(){
    return $this->hasMany('App\Models\AccountingModule\EntryItemModel','ledger_id');
}

public function debit()
{
    return $this->entries()->selectRaw('sum(amount) as debit')->where('dc','D');
}

In My controller:

$ledger = LedgerModel::with('entries')->with('debit')->where('id',81)->get(); 

Entries give the correct values but debit returns empty. I have printed query. It gives:

array:3 [▼
"query" => "select sum(amount) as debit from `entry_items` where `dc` = ? and `entry_items`.`ledger_id` in (?)"
"bindings" => array:2 [▼
  0 => "D"
  1 => 81
]
"time" => 0.79

I have executed the following query by pesting from print in sql and it gives value.

select sum(amount) as debit from `entry_items` where `dc` = "D" and `entry_items`.`ledger_id` in (81)

Please help me to find out my mistake.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire