技術情報

知っておいていただきたいこと – 4

今回も、Laravelの知っておいた方がいいとおもったことをいくつか紹介します。

Laravel Tip

ここでは、クエリを少し読みやすくするための小さな工夫を紹介します。あるオブジェクトに「属している」レコードを問い合わせる際には、 ‘whereBelongsTo’ メソッドを使用します。

$article = Article::where('user_id', $user->id)->paginate(10);

//After

$article = Article::whereBelongsTo($user)->paginate(10);

ということで、今回はこれで終わります。

金曜担当 – Ami



aws ec2環境でphpアップロードファイルのサイズ上限を変更する

今回はaws ec2環境でphpアップロードファイルのサイズ上限を変更する方法を
シェアしたいと思います。

続きを読む

Encrypt and Decrypt files with GPG in linux

Today, I would like to share about encrypting and decrypting files with GPG in linux. Let’s take a look.

GPG, known as GNU Privacy Guard (GnuPG), is an open-source command-line tool to encrypt things like files, emails, messages and so on.

Installation

GPG usually pre-installed with most of the Linux Distributions. But just in case, you can install by the following command in terminal.

sudo apt install gnupg

Usage

Generate keys

To encrypt files, you need to generate a GPG key on the system firstly.

gpg –full-generate-key

If the options to select are asked, you can read and use default values by pressing Enter.

But you will need to fill name and email.

Checking GPG keys

You can also check your key lists as follows.

Encrypting a file

Firstly, let’s create a sample text file with some text.

And let’s encrypt a file by the following command.

The recipient argument is the email you wrote when creating key.

The above command output the encrypted file with .gpg extension. You can delete original file after encryption.

And we can see the contents of the encrypted file as follow.

Decrypting a file

You can decrypt an encrypted gpg file by the following command.

This command outputs a decrypted file named decrypted_test.txt. Now you can see correct text contents of the file.

So this is all for now and for more details, I recommend to read here.

Hope you enjoy that.

By Asahi



知っておいていただきたいこと – 3

今回も、Laravelの知っておいた方がいいとおもったことをいくつか紹介します。

Tip # Laravel #exclude

入力を受け付けると、そのデータを使ってモデルを作りたいと思うことがよくあります。
例えば、登録フォームはUserモデルを作成するために使われるでしょう。しかし、すべてのフィールドを検証する必要がある一方で、モデルを作成するためにすべてのフィールドを必要としないかもしれません。このような場合、「exclude」ルールを使用します。

class StoreRequest extends FormRequest
{
  public function rules(){
      return [
         'name'                    => 'required|string',
         'email_address'           => 'required|string|email',
         'terms_and_conditions'    => 'required|accepted|exclude',    
       ];
  }
}

exclude’ ルールを使用すると、Laravel は ‘terms_and_conditions’ フィールドが全てのバリデーションルールをパスすることを保証しますが、 ‘validate’ または ‘validated’ メソッドによって返されるわけではありません。これにより、DBに’terms_and_conditions’のカラムがないことを気にせずに、簡単にモデルを作ることができます。

class RegistrationController extends Controller 
{
     public function store(StoreRequest $request)
     {
        $playload = $request->validate(); //名とメールだけ
        $user = User::create($playload);
        Auth::login($user);
        return redirect()->route('dashboard');
     }
}

ということで、今回はこれで終わります。

金曜担当 – Ami



A simple script to send email by SMTP in Python

Today, I would like to share about sending emails by SMTP with a simple python script. Let’s take a look.

To send smpt emails, we will need just one library smtplib that is a built-in python lib. Here is the code.

There are just simple 6 steps.

1. Connect to a SMTP server [line 3]

2. Sending the SMTP ‘Hello’ Message [line 4]

3. Starting TLS Encryption [line 5]

4. Logging in to the SMTP server [line 7]

5. Sending an Email [line 8]

6. Disconnecting from the SMTP server [line 11]

Note: To send with gmail SMTP, you will need to turn on ‘Less secure app access’ in your gmail account settings.

This is all for now. Hope you enjoy and can expand to further uses based on that.

By Asahi




アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム