Skip to content

Instantly share code, notes, and snippets.

View grim-reapper's full-sized avatar

Imran Ali grim-reapper

View GitHub Profile
@grim-reapper
grim-reapper / query.php
Created October 22, 2020 19:35 — forked from reinink/query.php
Getting table totals using Laravel Eloquent
<?php
$totals = DB::table('subscribers')
->addSelect(DB::raw('count(*) as all'))
->addSelect(DB::raw("sum(case when status = 'confirmed' then 1 else 0 end) as confirmed"))
->addSelect(DB::raw("sum(case when status = 'unconfirmed' then 1 else 0 end) as unconfirmed"))
->addSelect(DB::raw("sum(case when status = 'cancelled' then 1 else 0 end) as cancelled"))
->addSelect(DB::raw("sum(case when status = 'bounced' then 1 else 0 end) as bounced"))
->addSelect(DB::raw("sum(case when status = 'inactive' then 1 else 0 end) as inactive"))
->get();

Laravel Developer - Interview Exercise

  • Setup a fresh Laravel 5.8 application.
  • Use Tailwind CSS as the CSS framework.
    • CDN version is fine.
    • Bonus points if installed via NPM.
  • Create a new contact page at /contact.
  • Create a contact form on the page.
  • The form should be built as a Vue component.
  • The form should submit via ajax.
@grim-reapper
grim-reapper / eloquent-cheatsheet.php
Created October 15, 2020 17:39 — forked from hassansin/eloquent-cheatsheet.php
Laravel 5 Eloquent CheatSheet #laravel #eloquent
Model::
/*Select*/
select('col1','col2')
->select(array('col1','col2'))
->select(DB::raw('businesses.*, COUNT(reviews.id) as no_of_ratings, IFNULL(sum(reviews.score),0) as rating'))
->addSelect('col3','col4')
->distinct() // distinct select
/*From*/
@grim-reapper
grim-reapper / js_obj_getter_by_reg.js
Created July 28, 2020 12:54 — forked from sourcec0de/js_obj_getter_by_reg.js
Select keys from a javascript object using a regular expression. Returns an object containing only the keys that matched the expression in the original object.
var keyMatch = function(o,r){
var c = 0;
var nO = {};
Object.keys(o).forEach(function(k){
c++;
no[k] = k.match(r) ? o[k] : void 0;
});
@grim-reapper
grim-reapper / controller.php
Created June 23, 2020 07:08 — forked from jnbn/controller.php
Replicate (Duplicate) Eloquent Model With Relations
<?php
public function replicateWithRelations(QuestionCategory $questioncategory)
{
$newCategory = $questioncategory->replicate();
$newCategory->name = "Kopyası: ".$questioncategory->name;
$newCategory->push();
$questioncategory->relations = [];
//load relations on EXISTING MODEL
@grim-reapper
grim-reapper / SaveQuietly.php
Created May 15, 2020 17:29 — forked from janzikmund/SaveQuietly.php
Laravel eloquent trait to add method for save model without triggering observers
<?php
namespace App\Traits;
/**
* Allows saving models without triggering observers
*/
trait SaveQuietly
{
/**
* Save model without triggering observers on model
@grim-reapper
grim-reapper / swaparrayelements.js
Created April 26, 2020 10:55 — forked from eerohele/swaparrayelements.js
Swap two array elements (JavaScript)
/**
* Swap the elements in an array at indexes x and y.
*
* @param (a) The array.
* @param (x) The index of the first element to swap.
* @param (y) The index of the second element to swap.
* @return {Array} The input array with the elements swapped.
*/
var swapArrayElements = function (a, x, y) {
if (a.length === 1) return a;
@grim-reapper
grim-reapper / short-number-format.php
Created January 28, 2020 08:01 — forked from RadGH/short-number-format.php
Short Number Formatter for PHP (1000 to 1k; 1m; 1b; 1t)
<?php
// Converts a number into a short version, eg: 1000 -> 1k
// Based on: http://stackoverflow.com/a/4371114
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
@grim-reapper
grim-reapper / commands.sh
Created October 21, 2019 09:33 — forked from phplaw/commands.sh
All about docker
# List all containers (only IDs)
docker ps -aq
# Stop all running containers
docker stop $(docker ps -aq)
# Remove all containers, maybe we need -f| --force option for force delete running container
docker rm $(docker ps -aq)
# Remove all images
<?php
function time2str($ts) {
if(!ctype_digit($ts)) {
$ts = strtotime($ts);
}
$diff = time() - $ts;
if($diff == 0) {
return 'now';
} elseif($diff > 0) {
$day_diff = floor($diff / 86400);