5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel 共通関数作成

Last updated at Posted at 2022-03-10

コントローラー等で使いたい共通関数の作成方法。
app/ 直下にフォルダを作成しその中にファイルを作成。今回はLibraryフォルダを作成した。

app/Library/Hoge.php
<?php

namespace App\Library;

use Illuminate\Support\Facades\Facade;

class Hoge extends Facade
{
    public function fuga ()
    {
        //処理を書く
    }

}

エイリアス登録。
登録はしなくてもuse使用すればOK。

config/app.php
'Hoge' => App\Library\Hoge::class,

コントローラーから呼び出し

app/Http/Controllers/UserController.php
\Hoge::huga();
5
4
1

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?