{blog}


  • Serve Laravel to the web

    Serve Laravel to the web

    This will make your Laravel instance available on the web. Make sure your router have port 80 forwarded to your machine. Also make sure no other server applications is blocking the port.

    php artisan serve --host 0.0.0.0 --port 80
    Bash


  • Sort array of objects by property value

    Sort array of objects by property value

    usort($myArray, function ($a, $b) {
        return strcmp($a->myPropery, $b->myPropery);
    });
    PHP

    Sort by predefined order:

    $predefinedOrder = [1, 5, 2, 6];
    
    usort($arrayOfObjects, function ($a, $b) use ($predefinedOrder) {
    
        $flipped = array_flip($predefinedOrder);
        $left = $flipped[$a->myPropery];
        $right = $flipped[$b->myPropery];
        return $left >= $right;
    });
    PHP


  • Add column to table in Laravel

    Add column to table in Laravel

    Create migration in console:

    php artisan make:migration add_mycolumn_to_mytable
    Bash

    Use Schema::table() to access existing table (instead of Schema::create() for creating new tables)

    public function up()
    {
        Schema::table('mytable', function($table) {
            $table->text('mycolumn');
        });
    }
    public function down()
    {
        Schema::table('mytable', function($table) {
            $table->dropColumn('mycolumn');
        });
    }
    PHP

    Then run migrations:

    php artisan migrate
    Bash

  • Allow Composer to connect to http/https

    Allow Composer to connect to http/https

    If you have problems with Composer not allowing to update to http-connections add this to composer.json (not recommended to keep in production)

    config : {
      "secure-http" : false
    }
    JSON


  • (WordPress) Generate post title from custom field

    I had a custom post type that only used custom fields, so I needed to generate a post title from there.
    Here’s one way. You can if course chain as many if else as you want to check other types.

    functions.php
    [php]
    function custom_post_type_title($post_id)
    {
    global $wpdb;
    if (get_post_type($post_id) == ‘staff’) {
    $name = get_post_custom_values(‘name’);
    $title = $name[0];
    $where = array(‘ID’ => $post_id);
    $wpdb->update($wpdb->posts, array(‘post_title’ => $title), $where);
    }
    }

    add_action(‘save_post’, ‘custom_post_type_title’);

    [/php]


  • Laravel helpful plugins

    Laravel helpful plugins

    Generate Seed from Database
    https://github.com/orangehill/iseed

    Generate Migrations from Database:
    https://github.com/Xethron/migrations-generator


  • (JS) Newline to br

    Replaces newline characters with <br> tags in the same style as the php nl2br function

    // newline to br - php style
    function nl2br(str, is_xhtml) {
        let breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
        return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
    }
    JavaScript


  • (Ruby) Ruby on Rails doodles

    Create new Ruby-on-Rails project. By default RoR comes with SQLite support, unless you state otherwise (-d mysql).
    [ruby]
    // Create project. With mysql support
    rails new my_project -d mysql
    [/ruby]


  • (MySQL) MySQL doodles

    [sql]
    — Replace a word or string in a column
    UPDATE my_table SET my_column = REPLACE(my_column, ‘Old string’, ‘New string’);


    [/sql]


  • (JS) Super simple slider/carousel/slideshow

    Here’s how you can make a super simple slideshow that changes slides on a timer interval. You could easily edit it so any other event, like a mouse click, will trigger the slide() function.

    You can see it in action HERE

    HTML

    [html]
    <div class="slider">

    <div class="slide red">
    <p>THIS IS SLIDE 1</p>
    </div>

    <div class="slide blue">
    <p>THIS IS SLIDE 2</p>
    </div>

    <div class="slide green">
    <p>THIS IS SLIDE 3</p>
    </div>

    </div>
    [/html]

    CSS

    [css]
    .slider {
    width: 500px;
    height: 300px;
    margin: 0 auto;

    /* DON’T CHANGE THESE */
    position: relative;
    overflow: hidden;
    }

    .slide {
    /* DON’T CHANGE THESE */
    position: absolute;
    width: 100%;
    height: 100%;
    }

    /** ONLY FOR STYLING THE EXAMPLE SLIDES */
    .slide p {
    color: #FFF;
    text-align: center;
    }

    .red {
    background: red;
    }

    .blue {
    background: blue;
    }

    .green {
    background: green;
    }
    [/css]

    jQuery

    [js]
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script>
    // Configurable values
    slider = $(‘.slider’); // Slider element
    slideDuration = 3000; // Duration of each slide in milliseconds
    slideSpeed = 1000; // Speed of slide animation in milliseconds (must be equal of less than slideDuration)

    width = slider.width();
    slider.children().hide().css({left: width});
    slider.children(‘:first’).show().css({left: 0});

    // Slide in from the left
    $.fn.slideIn = function () {
    $(this).show().animate({
    left: "-=" + width
    }, slideSpeed / 2);
    }

    // Slide out to the left
    $.fn.slideOut = function () {
    $(this).animate({
    left: "-=" + width
    }, slideSpeed / 2, function () {
    $(this).hide().css({left: width});
    });
    }

    // Main slide function
    function slide() {
    currentSlide = slider.find(‘div:visible:first’);
    nextSlide = (!slider.children(":last").is(":visible")) ? currentSlide.next() : slider.children(‘:first’);

    currentSlide.slideOut();
    nextSlide.slideIn();
    }

    // Timer function
    window.setInterval(function () {
    slide();
    }, slideDuration);
    </script>
    [/js]

    And of course you could make this dynamic by spitting out the slides from a database… for example like this:

    [php]
    <div class="slider">

    <?php foreach ($slides as $slide) { ?>

    <div class="slide">
    <p><?php $slide[‘content’] ?></p>
    </div>

    <?php } ?>

    </div>
    [/php]