0
0

More than 1 year has passed since last update.

【Laravel】取得した配列が空かどうかを判別し、空の場合メッセージを返すメソッド

Last updated at Posted at 2022-04-13

1. confirmEmptyArrayメソッド

App/Lib/MyFunc.php
<?php

namespace App\Lib;

class MyFunc 
{
    public static function confirmEmptyArray($array, $empty_message)
    {   
        if (empty($array)) {
            return $empty_message;
        } else {
            return $array;
        }   
    }
}   

2. 使い方

App/Controllers/HomeController.php
<?php

namespace App\Http\Controllers;

use App\Lib\MyFunc; 

class HomeController extends Controller
{
    public function *** {
        $array = Model::where()-> ......->get()->all();

        $array = MyFunc::confirmEmptyArray($array, '空です');
    }
}
0
0
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
0