我在 Utilities.php 中有一個看起來像這樣的函式
namespace App\Utilities;
function generateHash()
{
return bin2hex(random_bytes(64));
}
并在遷移檔案中呼叫
<?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()
{
}
}
我收到一條錯誤訊息“呼叫未定義的函式 generateHash()”。
如何正確參考我正在匯入的函式?
uj5u.com熱心網友回復:
因為您在其他 php 檔案中使用方法 generateHash(),所以您應該在遷移檔案中要求(或使用)。你應該做這樣的事情:
use function App\Utilities\generateHash;
我希望它會起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513540.html
標籤:php拉拉维尔
