How to call method inside the controller?

Published 4 months ago by AquinoBR

Yesterday I made a post asking how to integrate the class of BlockIo in Laravel. I was able to install via composer and do this procedure. But I can not call the external class methods inside my existing controller at all. Could someone show me how to call a method inside the controller? I think the problem is in linking the install class to the controller. Thanks for any reply.

Installed package and tutorial: https://github.com/blockavel/lara-block-io

I did all procedure until Publish Vendor.

Best Answer (As Selected By AquinoBR)
Sergiu17
use Blockavel\LaraBlockIo\LaraBlockIo;

I've tested, it works


<?php

return [

    'providers' => [
        Blockavel\LaraBlockIo\LaraBlockIoServiceProvider::class,
    ],

    'aliases' => [
        'LaraBlockIo' => Blockavel\LaraBlockIo\LaraBlockIoFacade::class,
    ],

];

shez1983

and what is your code in your controller?

Sergiu17

What is the error?

AquinoBR

@shez1983

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\BlockIo;


class BalanceController extends Controller
{

    public function depositBTC()
     {
        return view('admin.balance.depositBTC');
        }

     public function depositBTCCreate(Request $request, BlockIo $blockio)
    {
        $user = auth()->user();

        if ($user->addressbtc == null){

             $label = $user->id;
         //create address btc
             $newAddressInfo = $blockio->CreateAdressBtc($label);

              if ($newAddressInfo['success'])
          return redirect()
                            ->route('deposit.brl')
                            ->with('success', $newAddressInfo['message']);

    return redirect()
                          ->back()
                          ->with('error', $newAddressInfo['message']);
          
          } else {
              return redirect()
                    ->back()
                  ->with('error', 'Você já possui um endereço de depósito Bitcoin.');
          } 
  }
}

Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BlockIo extends Model
{
    /**
     * Get the balance information associated with a Bitcoin Dogecoin,
     * or Litecoin account.
     *
    * @return object Contains balance information
    */
   
   public function CreateAddressBtc()
   {
       return LaraBlockIo::createAddress($label); 
   }
}

@Sergiu17 -> Erro: "Class 'App\Models\LaraBlockIo' not found"

mcangueiro

Are you setting an alias for LaraBlockIo in your config/app.php file? If not you need to import the LaraBlockIo in your model in order to use it.

Also, in your model you're not passing any argument to the CreateAddressBtc function but in your controller you are.

Sergiu17

@AquinoBR In your BlockIo Model add:

use Blockavel\LaraBlockIo\LaraBlockIoFacade;

Hope it will work..

Like @mcangueiro said, you have to import LaraBlockIo

AquinoBR

@mcangueiro Yes I added in config / app.php ...

Blockavel \ LaraBlockIo \ LaraBlockIoServiceProvider :: class, 'LaraBlockIo' => 'Blockavel \ LaraBlockIo \ LaraBlockIoFacade', ...

@ Sergiu17 I added the line in Model BlockIo and the error remains the same.

mcangueiro

Do you mind sharing your aliases and providers sections of your config/app.php? Use the Markdown block for that please.

AquinoBR

@mcangueiro, file config/app.php

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */
        PragmaRX\Google2FALaravel\ServiceProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Blockavel\LaraBlockIo\LaraBlockIoServiceProvider::class,

    ],

'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
        'Bus' => Illuminate\Support\Facades\Bus::class,
        'Cache' => Illuminate\Support\Facades\Cache::class,
        'Config' => Illuminate\Support\Facades\Config::class,
        'Cookie' => Illuminate\Support\Facades\Cookie::class,
        'Crypt' => Illuminate\Support\Facades\Crypt::class,
        'DB' => Illuminate\Support\Facades\DB::class,
        'Eloquent' => Illuminate\Database\Eloquent\Model::class,
        'Event' => Illuminate\Support\Facades\Event::class,
        'File' => Illuminate\Support\Facades\File::class,
        'Gate' => Illuminate\Support\Facades\Gate::class,
        'Hash' => Illuminate\Support\Facades\Hash::class,
        'Lang' => Illuminate\Support\Facades\Lang::class,
        'Log' => Illuminate\Support\Facades\Log::class,
        'Mail' => Illuminate\Support\Facades\Mail::class,
        'Notification' => Illuminate\Support\Facades\Notification::class,
        'Password' => Illuminate\Support\Facades\Password::class,
        'Queue' => Illuminate\Support\Facades\Queue::class,
        'Redirect' => Illuminate\Support\Facades\Redirect::class,
        'Redis' => Illuminate\Support\Facades\Redis::class,
        'Request' => Illuminate\Support\Facades\Request::class,
        'Response' => Illuminate\Support\Facades\Response::class,
        'Route' => Illuminate\Support\Facades\Route::class,
        'Schema' => Illuminate\Support\Facades\Schema::class,
        'Session' => Illuminate\Support\Facades\Session::class,
        'Storage' => Illuminate\Support\Facades\Storage::class,
        'URL' => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View' => Illuminate\Support\Facades\View::class,
        'Google2FA' => PragmaRX\Google2FALaravel\Facade::class,
        'LaraBlockIo' => 'Blockavel\LaraBlockIo\LaraBlockIoFacade',

    ],

mcangueiro

Try replacing:

'LaraBlockIo' => 'Blockavel\LaraBlockIo\LaraBlockIoFacade',

with

'LaraBlockIo' => Blockavel\LaraBlockIo\LaraBlockIoFacade::class,
Sergiu17
use Blockavel\LaraBlockIo\LaraBlockIo;

I've tested, it works


<?php

return [

    'providers' => [
        Blockavel\LaraBlockIo\LaraBlockIoServiceProvider::class,
    ],

    'aliases' => [
        'LaraBlockIo' => Blockavel\LaraBlockIo\LaraBlockIoFacade::class,
    ],

];

AquinoBR

My friends, I made the changes and now I received a new error:

"Non-static method Blockavel \ LaraBlockIo \ LaraBlockIo :: createAddress () should not be called statically"

mcangueiro

@Sergiu17 They have made a small mistake in their documentation. The aliases bit should be like what you have posted.

mcangueiro

@AquinoBR That is related to what I pointed in my first reply. You are not passing any argument to your CreateAddressBtc in your model, but you are calling a variable $label in there.

AquinoBR

@mcangueiro I do not know what to do, I'm trying to create a function to create an address. Could you help me with an example application of this function please?

Sergiu17

@AquinoBR

$obj = new LaraBlockIo();
$bbj->createAddress();

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