Saturday, February 25, 2017

Convert to camel case and snake case in php

Convert to camel case and snake case in php:

In php, if we want to convert camel case to snake case and vise versa then we have to use following methods:

public static function convertToCamelCase($array) {
        $converted_array = [];
        foreach ($array as $old_key => $value) {
            if (is_array($value)) {
                $value = static::convertToCamelCase($value);
            } else if (is_object($value)) {
                if (method_exists($value, 'toArray')) {
                    $value = $value->toArray();
                } else {
                    $value = (array) $value;
                }
                $value = static::convertToCamelCase($value);
            }
            $converted_array[camel_case($old_key)] = $value;
        }
        return $converted_array;
    }


public static function convertToSnakeCase($array) {
        $converted_array = [];
        foreach ($array as $old_key => $value) {
            if (is_array($value)) {
                $value = static::convertToSnakeCase($value);
            } else if (is_object($value)) {
                if (method_exists($value, 'toArray')) {
                    $value = $value->toArray();
                } else {
                    $value = (array) $value;
                }
                $value = static::convertToSnakeCase($value);
            }
            $new_key = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $old_key)), '_');
            $converted_array[$new_key] = $value;
        }
        return $converted_array;
    }



This will help for this.

Laravel 5.4 validation for age lesser than 18

Laravel 5.4 validation for age lesser than 18:

In laravel 5.4 validation we have only min & max value , which counts the number.

If in such case we have to validate age then this will not help.

For this we have to use regex like this:

            'age' => ['required','numeric','regex:/^(?:[1-9]\d{2,}+|[2-9]\d|1[89])$/i'],

It will not allow age lesser than 18 

Thanks

Laravel multi select box get old value

Laravel multi select box get old value:

Hi, Some time we want to get old values in laravel (when there is some validation apply on a form.).

In this case, genrally we are unable to get old value for a multiple select box.

For this use this code:

<select name="tribe[]" id="tags" class="form-control" multiple>
                                    @if (is_array(old('tribe')))
                                    @foreach ($tribes as $tribe)
                                    <option value="{{ $tribe->id }}" <?php if(in_array($tribe->id, old('tribe'))) {echo 'selected';} ?> >{{ $tribe->name }}</option>
                                    @endforeach
                                    @else
                                    @foreach ($tribes as $tribe)
                                    <option value="{{ $tribe->id }}" >{{ $tribe->name }}</option>
                                    @endforeach
                                    @endif
                                </select>


This will help for get old values in multiple select box.

Thanks