php


  • Impersonate users with Sanctum in Laravel

    Impersonate users with Sanctum in Laravel

    I needed to write a somewhat clean solution to let an admin impersonate other users. Which basically means that one user can appear as another user – without having to get access that users credentials to log in. The most obvious use case for this would be when an admin needs access to a user’s…

  • (PHP) Serve Laravel to the web

    (PHP) 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] php artisan serve –host 0.0.0.0 –port 80 [/php]

  • (PHP) Add column to table in Laravel

    Create migration in console: [php] php artisan make:migration add_mycolumn_to_mytable [/php] Use Schema::table() to access existing table (instead of Schema::create() for creating new tables) [php] 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] php artisan migrate [/php]

  • (PHP) 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) [js] config : { "secure-http" : false } [/js]

  • (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’);…

  • (PHP) Laravel helpful plugins

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

  • (PHP) Get a list of run database queries in Laravel

    This is great if you want to see what queries are actually run when using Eloquent. [php] // Get all querys run $queries = DB::getQueryLog(); // If you want to sort them by time this works usort($queries, function ($a, $b) { return $a[‘time’] < $b[‘time’]; }); // Print them on screen in a readable way…

  • (PHP) Log Laravel execution time to console

    Put this in app/start/global.php to get Laravels execution time to the browser console log. L4 [php] $start = microtime(true); App::finish(function() use ($start) { echo "<script>console.log(‘App finish: ".round((microtime(true) – $start) * 1000, 3)." ms’)</script>"; }); [/php] This works with L5: [html] This page took {{ (microtime(true) – LARAVEL_START) }} seconds to render [/html]

  • (HTML) Form element arrays

    Sometimes you might want to group form elements in arrays. Here’s a way to do that: [html] <form> <input type="text" name="textboxes[]"> <input type="text" name="textboxes[]"> <input type="text" name="textboxes[]"> </form> [/html] The structure of the $_POST array will then be: [php] Array ( [textboxes] => Array ( [0] => value 1 [1] => value 2 [2] =>…

  • (PHP) sorting an array on a specific value

    Simple way to sort an array with usort on a specific value using a comparator. [php] usort($myArray, function ($a, $b) { return $a[‘foo’] > $b[‘foo’]; }); [/php]