Skip to content

Instantly share code, notes, and snippets.

View cuonghuynh's full-sized avatar
🏠
Working from home

Cuong Huynh cuonghuynh

🏠
Working from home
View GitHub Profile
@cuonghuynh
cuonghuynh / code.md
Created October 2, 2018 14:53
Laravel: Pass and get the data via redirect response

Pass data:

return redirect()->route('admin.article.index')->with('results', $results);

Get data:

session('results');
@cuonghuynh
cuonghuynh / code.php
Created October 2, 2018 14:08
Detect delimiter of csv file
/**
* Class CsvHelpers
* @package App\Services\Common
*/
class CsvHelpers
{
/**
* @param $file
* @return string
*/
@cuonghuynh
cuonghuynh / guide.md
Last active October 1, 2018 15:55
How to Import a large CSV file to our storage?

Chunk data

If you have a large CSV file, you very likely can't store it all in an array in memory before you start inserting the data into the DB, so you need to be inserting the data in chunks as you read it from the CSV file.

Queue chunks

You would be lost the data if the process unfortinately broken.

Log errors

You should know why it is broken. :)

@cuonghuynh
cuonghuynh / solution.md
Last active October 1, 2018 03:31
Errors after upgrading mysql to 5.7

error-related-to-only-full-group-by-when-executing-a-query-in-mysq

As of MySQL 5.7.5, the default SQL mode includes ONLY_FULL_GROUP_BY which means when you are grouping rows and then selecting something out of that groups, you need to explicitly say which row should that selection be made from.

Fixing:

$ sudo vim /etc/mysql/my.cnf

Add them to bottom of file

@cuonghuynh
cuonghuynh / upgrade.md
Last active October 1, 2018 09:34
Install and upgrade MariaDB

Add MariaDB Repository

$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
$ sudo add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.2/ubuntu trusty main'

Update Repository index

$ sudo apt-get update
@cuonghuynh
cuonghuynh / sample.php
Created September 29, 2018 09:46
Laravel model where raw
$product->whereRaw("strftime('%Y-%m', created_at) = '2010-1'")->get();
@cuonghuynh
cuonghuynh / upgrade-mysql.md
Last active September 29, 2018 17:21
Upgrade mysql 5.5 to 5.7 on Ubuntu

First of all , Backup All your Database.

$ mysqldump --all-databases > all_databases.sql

Add the newer APT package repository from the MySQL APT repository page

$ wget https://dev.mysql.com/get/mysql-apt-config_0.8.10-1_all.deb

$ sudo dpkg -i mysql-apt-config_0.8.1-1_all.deb

Choose version 5.7 -> OK

Update your package index

@cuonghuynh
cuonghuynh / how-to-export-variables-for-frontend.md
Last active September 19, 2018 14:30
Backend exports variables for Frontend

Template engine:

Usually, we place it in script section of footer.

<script type="application/json" data-from-backend>
    {
      "product_id": "{{ product.id }}",
      "user_address": "{{ userAddress }}"
    }
</script>
@cuonghuynh
cuonghuynh / slugify.js
Created September 17, 2018 19:22
slugify title for vietnamese
str = str.toLowerCase();
str = str.replace(/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/g, 'a');
str = str.replace(/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/g, 'e');
str = str.replace(/(ì|í|ị|ỉ|ĩ)/g, 'i');
str = str.replace(/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/g, 'o');
str = str.replace(/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/g, 'u');
str = str.replace(/(ỳ|ý|ỵ|ỷ|ỹ)/g, 'y');
str = str.replace(/(đ)/g, 'd');
@cuonghuynh
cuonghuynh / gist:c3a2607617684a9ead245ad2221a9761
Last active September 12, 2018 05:19
Get memory usage in MB
echo round((memory_get_usage() / 1024 / 1024, 2) . ' MB' . PHP_EOL;