showing the extract of an account

Published 1 year ago by halil

Hi. I'm working on our own accounting system which includes cash, bank account, inventory etc. I have the tables for all these processes. Now i want to get the extract of a customer(or safe, supplier, stock, bank account). For customer, the related tables are cash_from_customers, sales, card_payments etc. The similar columns for all these tables are id, amount, date. How can i query and get below result for a certain customer?

the result what i want: http://imgur.com/a/ZtAFm

Best Answer (As Selected By halil)
EventFellows

There is a ton of helpers for collections: https://laravel.com/docs/5.4/collections#available-methods

you probably need something like merge() and then map()

Ruffles
Ruffles
1 year ago (294,940 XP)

What are the relationships between the tables?

halil
halil
1 year ago (15,020 XP)

i prepared an example diagram

http://imgur.com/a/Re4ZP

EventFellows

If you setup your relationships on the different models as ->hasMany(), ->belongsTo() and so on you can just call them as you named them.

$customer = Customer::find(3);

$bankAccounts = $customer->bank_acccounts(); // in case you have a relationship defined as public function bank_accounts() for example

The docs will show you the details: https://laravel.com/docs/5.4/eloquent-relationships#one-to-many (obviously depending on what kind of relationsships you have)

halil
halil
1 year ago (15,020 XP)

no, i want the extract of an account. This must be a data table from several tables.

EventFellows

Well, then make query the relationships for each thing you want and combine the collections (sort and filter it as you like)

If all the collumns are called the exact same name (e.g. the amout is always called 'amount' on the tables) then this should instantly work. Otherwise create different methods on each model.

E.g.

public function getAmount() {
    $this->amount;  // if collumn is called 'amount'
}
public function getAmount() {
    $this->value;   // if collumn is called 'value'
}

Then you can run a foreach loop and call ->getAmount() on each object.

halil
halil
1 year ago (15,020 XP)

@EventFellows you said "combine the collections (sort and filter it as you like)". This is the important point for me.

$cfc = CashFromCustomer::find($customerid); $sales = Sales::find($customerid); $cp = CardPayments::find($customerid);

after that, i must combine these collections and create also an extra columns (balance) to sum the amounts. (Alsa sales amount must be multiplied with -1)

EventFellows

There is a ton of helpers for collections: https://laravel.com/docs/5.4/collections#available-methods

you probably need something like merge() and then map()

halil
halil
1 year ago (15,020 XP)

I work with eloquent collections, i thought that methods are for array collections. Now will try it.

EventFellows

There is no difference. Collections are Collections and these helpers will work with it.

halil
halil
1 year ago (15,020 XP)

Thanks. I merged different objects and so i can't use in blade like this: $action->amount. It gives error. I must use $action['amount']. Am I right?

Please sign in or create an account to participate in this conversation.