Best Practices for Custom Helpers in Laravel 5

后端 未结 20 2017
暖寄归人
暖寄归人 2020-11-22 06:40

I would like to create helper functions to avoid repeating code between views in Laravel 5:

view.blade.php

Foo Formated text: {{ fo

20条回答
  •  长发绾君心
    2020-11-22 07:24

    **

    • Status Helper

    ** create new helper

     [
                'value' => 1,
                'displayName' => 'Active',
            ],
            2 => [
                'value' => 2,
                'displayName' => 'Inactive',
            ],
            3 => [
                'value' => 3,
                'displayName' => 'Delete',
            ],
    
        ];
    
         public static function getStatusesList()
        {
            $status = (new Collection(self::$_status))->pluck('displayName', 'value')->toArray();
    
    
            return $status;
        }
    }
    

    Use for the controller and any view file

    use App\Helpers\StatusHelper;
    
    class ExampleController extends Controller
    {
            public function index()
            {
                $statusList = StatusHelper::getStatusesList();
    
                return view('example.index', compact('statusList'));
            }
    }
    

提交回复
热议问题