0
1

More than 3 years have passed since last update.

自作関数の作り方

Last updated at Posted at 2020-12-02

クラスの作成

app以下にLibraryディレクトリを作ってFunctions.phpを作成。(ディレクトリの場所はどこでもいいです。)このファイルの中に自作メソッドを保管していきます。
staticメソッドのfunctionを作ります。

app/Library/Functions.php
class MyFunctions
{
    public static function xxxx()
    {
        //処理内容
    }
}

エイリアスの登録

config/app.php
config/app.php のaliasesに作成したクラスを追加します

'aliases' => [
    'MyFunctions' => App\Library\Functions::class,
   ],

コントローラーで使う

コントローラーにuse MyFunctionsで使用可能。呼び出すときは MyFunctions::xxxx()で。
エイリアスの登録を行わなかった場合はuse App\Lib\MyFunctions

Controller.php
use MyFunctions で使用可能
MyFunctions::xxxx() で呼び出して使います

bladeテンプレートで使う

blade.php
{{ MyFunctions::hoge() }} で呼び出して使えます
@foreach (MyFunctions::xxxx() as $zzz) ... {{$zzz}} @endforeach

0
1
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
1