#TIL : FTP via curl tool

Can upload an file via FTP by curl tool with handy script :

1
curl -T file_need_to_upload ftp://hostname --user user:passwd

#TIL : Eloquent Many-to-Many Relationship

To create the n-to-m relationship in Eloquent, we create a table stand between 2 tables.
Eg:

We have the db schema :

post post_tag tag
id (PK) id (PK) id (PK)
title post_id (Index) name (Unique)
content tag_id (Index)

In the model Post and Tag, we define a relation :

  • App\Model\Post.php
1
2
3
public function tags() {
return $this->belongsToMany('App\Model\Tag', 'post_tag', 'post_id', 'tag_id');
}
  • App\Model\Tag.php
1
2
3
public function posts() {
return $this->belongsToMany('App\Model\Post', 'post_tag', 'tag_id', 'post_id');
}

After that, we can use the relation to fetch the related data, like

1
2
$post = Post::firstOrFail(1);
$tags = $post->tags; // return the Collections class contains tags

or

1
2
$tag = Tag::where('name', '=', 'php')->first();
$posts_of_tag = $tag->posts; // return the Collections class contains posts

When updating or creating post, we have to sync with tags by

1
2
$tag_ids = [2, 3 ,4]; // Get tag ids from tag names
$post->tags()->sync($tag_ids);

#TIL : Index is useless when use function on indexed field

Reality, if using function on indexed field, you will broke indexing by accident.
Eg:

1
WHERE MONTH(`date`) = 11 AND YEAR(`date`) = 2015

Solution is transform the query to comparison query, like this :

1
WHERE `date` >= '2015-11-01' AND `date` < '2015-12-01'

#TIL : String Format Unicode params

1
2
unicode_thing = u"Xin chào mọi người"
a = '{}'.format(unicode_thing)

will cause the error UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 6: ordinal not in range(128)

The solution is add u prefix the pattern (it means using unicode pattern) :

1
2
unicode_thing = u"Xin chào mọi người"
a = u'{}'.format(unicode_thing)

Today I Learned - automated tool helps you learn everyday.

Introduction

We constantly learn new things. This is a repo to share those learnings. TILs are short Markdown documents (a few sentences + example code) explaining concepts, bits of syntax, commands, or tips we’ve recently learned.

Today-I-Learn (TIL) is inspired by Thoughbot, TIL is a repository for everyone to share what we have learn today.

This repo has a tool that help you to manage and write down what you learned in scientific way. https://github.com/khanhicetea/today-i-learned

And this is my repo used TIL : https://github.com/khanhicetea/what-i-leaned-today

Instruction

Step 1 : Fork this repo (blank-repo with only tool and readme file).

Step 2 : Start writting down what you learned in everyday.

  • Create a topic
1
$ ./til [CATEGORY] [SUBJECT]

Ex:

1
$ ./til bash "Bash Conditional Expression"

It will create 1 file bash/Bash-Conditional-Expression.md

  • Write content
  • Save it !

Step 3 : Commit what you learned today

1
$ ./til commit

After that, if you want to push it to repository, just Enter.

License

© 2015 khanhicetea. Distributed under the Creative Commons Attribution License.

Ref

#TIL : Debugging Chrome extension

  • To open directly the Console Dev Tools, press Cmd + Opt + J in MacOSX.
  • To debug easily the any files of extension, open the url chrome-extension://<extension-id>/<file-name> in address bar.

#TIL : Shortcut keyboard improve productivity

  • Open the preference of any Application by Cmd + ,.
  • Press Cmd + ~ to go previous App when switching App on Cmd + Tab
  • Copy screenshot to clipboard by Cmd + Ctl + Shift + 3
  • Press Option when click to notification center is putting it on “Do no disturb” mode.