bavix / laravel-wallet

It's easy to work with a virtual wallet
https://bavix.github.io/laravel-wallet/
MIT License
1.14k stars 230 forks source link

How to eager load wallets with transactions? #986

Closed ISNewton closed 2 months ago

ISNewton commented 2 months ago

Describe the bug I need to eager load user wallets with their transactions :

        $students = Student::query()
            ->with('wallets.transactions')
            ->get();

I get the follwing error :

  TypeError: Bavix\Wallet\Services\CastService::getModel(): Argument #1 ($object) must be of type object, null given, called in /my_project_path/vendor/bavix/laravel-wallet/src/Services/CastService.php on line 55 and defined in /my_project_path/vendor/bavix/laravel-wallet/src/Services/CastService.php:58

Trace Error

  Stack trace:
  #0 /my_project_path/vendor/bavix/laravel-wallet/src/Services/CastService.php(55): Bavix\Wallet\Services\CastService->getModel()
  #1 /my_project_path/vendor/bavix/laravel-wallet/src/Traits/HasWallet.php(114): Bavix\Wallet\Services\CastService->getHolder()
  #2 /my_project_path/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(690): Bavix\Wallet\Models\Wallet->transactions()
  #3/my_project_path/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(106): Illuminate\Database\Eloquent\Builder->Illuminate\Database\Eloquent\{closure}()

Server:

rez1dent3 commented 2 months ago

Hello. transactions returns all transactions of all wallets by holder. You need to use walletTransactions.

        $students = Student::query()
-           ->with('wallets.transactions')
+           ->with('wallets.walletTransactions')
            ->get();

And for wallets, refer to the walletTransactions relation.

rez1dent3 commented 2 months ago

If you need all transactions of all wallets by holder, then use transactions.

        $students = Student::query()
            ->with('transactions')
            ->get();
ISNewton commented 2 months ago

I updated the code to :

        $students = Student::query()
            ->with(['wallets.walletTransactions'])
            ->get();

I got this error :

  TypeError: Bavix\Wallet\Internal\Events\WalletCreatedEvent::__construct(): Argument #1 ($holderType) must be of type string, null given, called in /my_project_path/vendor/bavix/laravel-wallet/src/Internal/Assembler/WalletCreatedEventAssembler.php on line 23 and defined in /my_project_path/vendor/bavix/laravel-wallet/src/Internal/Events/WalletCreatedEvent.php:17

I believe it is related to this issue: https://github.com/bavix/laravel-wallet/issues/613#issuecomment-1339657093

rez1dent3 commented 2 months ago

I believe it is related to this issue: https://github.com/bavix/laravel-wallet/issues/613#issuecomment-1339657093

The problems are a little different.

I got this error :

You need to update to at least laravel-wallet 10.0+ for this to work. Unfortunately, this won't work on versions below 10. I had to change the internal structure of the project very much.

Proof: https://github.com/bavix/laravel-wallet/commit/c6583a76050e7c48dad4da927d392083a1cb4a33 You can run this test locally.

Version 7.x has not been supported for almost two years.

screen

ISNewton commented 2 months ago

Thanks @rez1dent3 .