0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

laravelで自作の関数を作る

Posted at

自作関数を記述するファイルを作成

好きな所に自作関数を記述するファイルを作成。
app/functions.php

app/functions.php
<?php

// テスト関数
if (!function_exists('test')) {
    function test($date = null) {
        return 'testです!';
    }
}

オートローダーに追加

composer.json の autoload.files に追加

composer.json
    "autoload": {
        "files": [
            "app/functions.php" //追加
        ],

オートローダーの更新

$ composer dump-autoload

テスト

$ php artisan tinker
> test()
= "testです!"

呼び出し方

以下だけでコントローラーやbladeから自作関数を呼び出せるようになる。

test()
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?