Laravel auto deployment with GitLab CI/CD

I’ve been heard of continuous integration long ago, thus I decide to give it a try.
I found the official guideline from GitLab itself, and it looks quite complicated.
End up I follow this article.

Read More

window.onbeforeunload trigger ajax request

Initially I was using jQuery.ajax call to send a request to server before user
leave the page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
window.onbeforeunload = function(evt) {
const endpoint = '/lock/end-timestamp.php';
$.ajax({
url: endpoint,
dataType: 'json',
type: 'post',
async: false, // synchronous request
data: {
_token: $('meta[name="csrf-token"]').attr('content'),
param1: 'data1',
param2: 'data2',
},
});
};

Read More

Use Google Analytics API with Laravel 5

No doubt that Google is very powerful. I’ve been researching for
Google Analytics (GA) API for quite sometime, due to it’s complex documentation
(somehow I think is very hard to find what I need).

Read More

DataTable re-order rows

In CMS, often the navigation menu is editable in backend, and can change the order.
Let’s see how to implement using DataTable.

Read More

Laravel - Integrate oEmbed into TinyMCE

oEmbed is a kind of standard to retrieve 3rd party site info.

Example, in rich editor like TinyMCE, if want to embed
a YouTube video, what we usually do is

Read More

Laravel Eloquent join with subquery

Let’s take the query from this post as example

1
2
3
4
5
6
7
8
9
10
11
12
SELECT books.id AS book_id
, books.isbn
, books.title
, t_borrowers.user_ids
FROM books
LEFT JOIN (
SELECT user_books.book_id
, CONCAT('#', GROUP_CONCAT(user_books.user_id SEPARATOR '#,#'), '#') AS user_ids
FROM user_books
GROUP BY user_books.book_id
) AS t_borrowers ON t_borrowers.book_id = books.id
WHERE (t_borrowers.user_ids LIKE '%#1#%' OR t_borrowers.user_ids LIKE '%#3#%');

Read More

MySQL - a trick to filter multi-values column

For certain reason, sometime we want to filter the data in a multi-values column.

e.g.

books

Read More

Guzzle HTTP client cannot download file with special character

I’m using Guzzle to download image file, the code as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

$normal_url = 'http://example.com/path/to/image.png';
$special_char_url = 'http://example.com/path/to/ครีมอาบน้ำ.jpg';

function download($url) {
$output = '/tmp/' . basename($url);

$client = new \GuzzleHttp\Client();
$res = $client->get($url, [
'verify' => false,
'sink' => $output,
]);
}

download($normal_url); // this 1 works
download($special_char_url); // this throw 404 error

Read More

TinyMCE insert js/css into iFrame head element

TinyMCE is an iFrame element with full HTML content.

Sometime we would like to add in Bootstrap & jQuery or any plugins into it.
Let’s see how we achieve it.

Read More

AWS Lambda test in local machine

I believe many developer first time deal with AWS Lambda will have this question: “How can I test the code in my machines?”

After keep trying hundreds of times, I found a way to do it.

Read More