CoreProc / laravel-wallet-plus

Easily add a virtual wallet to your Laravel models. Features multiple wallets and a ledger system to help keep track of all transactions in the wallets.
MIT License
28 stars 10 forks source link

Call to a member function wallet() on string #5

Open gaberoonie opened 3 years ago

gaberoonie commented 3 years ago

I'm really enjoying the package, but I encounter the following issue.

When i submit the form, I get error ''Call to a member function wallet() on string"

Here is the code for the controller. The error occurs at: $userWallet = $user->wallet(1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroySendCtpPackageRequest;
use App\Http\Requests\StoreSendCtpPackageRequest;
use App\Http\Requests\UpdateSendCtpPackageRequest;
use App\Models\Package;
use App\Models\SendCtpPackage;
use App\Models\User;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use CoreProc\WalletPlus\Models\Traits\HasWallets;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Log;

class SendCtpPackageController extends Controller 
{   
    use Notifiable, HasWallets;

    public function create()
    {
        abort_if(Gate::denies('send_ctp_package_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $recipient_addresses = User::all()->pluck('ctp_address', 'id')->prepend(trans('global.pleaseSelect'), '');

        $package_types = Package::all()->pluck('package_name', 'ctp_value');

        return view('admin.sendCtpPackages.create', compact('recipient_addresses', 'package_types'));
    }

    public function store(StoreSendCtpPackageRequest $request)
    {   
        $sendCtpPackage = SendCtpPackage::create($request->all());
        $sendCtpPackage->package_types()->sync($request->input('package_types', []));

        $grandTotal = $request->input('grand_total', []);
        $recipient = $request->input('recipient_address_id', []);

        $user = $recipient;

        $userWallet = $user->wallet(1); // This method also accepts the ID of the wallet type as a parameter

        $userWalllet->incrementBalance(100);

        //$pesoWallet->balance;

        return redirect()->route('admin.send-ctp-packages.index');
    }
}

Thanks so much for assistance!

myraoliveros-cp commented 3 years ago

Hi @gaberoonie, you should add the HasWallets trait to your User model not in the controller. Once you fix that, correct this part too

$recipient = $request->input('recipient_address_id', []);               
$user = $recipient;

Based on the error message, $recipient has a value of string and you're copying that value to $user. $user should be an instance of a model with the HasWallets trait in order for you to call $user->wallet()