Home > other >  Laravel call to undefined function in another function
Laravel call to undefined function in another function

Time:10-11

I have a function in Utilities.php that looks like this

namespace App\Utilities;

function generateHash()
{
    return bin2hex(random_bytes(64));
}

and is called in the migration file

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

use App\Utilities\{generateHash};

use App\Models\{Foo};

class SomeMigration extends Migration
{
    public function up()
    {

        DB::transaction(function() {

            $foo_query = Foo::whereNull('hash', NULL)->get();

            foreach ($foo_query as $row)
            {
                Foo::where('id', $row->id)->update('hash', generateHash());
            }

        });
    }

    public function down()
    {

    }
}

I get an error message "Call to undefined function generateHash()".

How can I appropriately reference the function I am importing?

CodePudding user response:

Because you are Using method generateHash() in other php file , you should require( or use ) in your migration file. you should do some thing like this at :

use function App\Utilities\generateHash;

I hope it will work .

  • Related