アプリ関連ニュース

DataTablesを使用したテーブル生成とサーバーサイド連携(5)

DataTablesを使用したテーブル生成方法とサーバーサイド連携方法をシェアしたいと思います。
今回はサーバーサイド側の処理でデータベースから取得したレコードをDataTables表示に反映させる部分を説明いたします。

データベースから取得したレコードをDataTables表示に反映

以下のようにレコード内容がa,b,c,d,e固定になっていた部分をテーブルから取得した
実際のレコードに書き換えます。

function user_datatables(Request $request) {

    $rec_data = [];
    //テーブルから取得した実際のレコードに書き換え
    $query = Member::query();
    $member_list = $query->get();
    foreach($member_list as $member){
        $row_data = [
            'item1' => $member->id,
            'item2' => $member->sei.$member->mei,
            'item3' => $member->sei_kana.$member->mei_kana,
            'item4' => $member->mail,
            'item5' => $member->created_at->format('Y-m-d H:i'),
        ];
        $rec_data[] = $row_data;
    }

    $draw = $request->draw;

    $res_json = array(
        'draw' => $draw,
        'recordsTotal' => '57', 
        'recordsFiltered' => '57', 
        'data' => $rec_data, 
    );

    return response()->json($res_json);
}

以下のようにテーブルから取得したレコードがDataTablesに反映されていることが確認できました。

ページング処理の追加

次にページング処理の作成方法を説明いたします。

サーバーサイドでページング処理を実装するために
・1ページあたりの表示件数
・何件目から表示するか
のデータが必要になりますが、これらのデータはDataTablesがpostしてくれます。
以下のように取得できます。

//1ページあたりの表示件数
$length = $request->length;

//何件目から表示するか
$start = $request->start;

Laravelでレコードの抽出処理をおこなう際に、それぞれの数値をlimitおよびoffsetに設定します。

$query->offset($start);
$query->limit($length);

またフロント側でページ番号の表示処理をおこなうためにサーバーサイドからレコードの全件件数を
返す必要がありますが、これはLaravelの以下のメソッドで取得できます。

//全件件数の取得
$total_cnt = $query->count();

次回は上記の設定をおこなったページネーションのサーバーサイド処理の説明をおこないたいと思います。

木曜日担当:nishida



Windows用の Direct Storage API がリリースされました

XBOX series X では既に利用されていますが、
約1年程遅れてWindowsでも利用可能になるようです。

続きを読む

Django REST Framework

Today I would like to share a brief explanation about Django REST framework. Let’s take a look.

Django REST framework is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django’s class-based views. It adopts implementations such as class-based views, forms, model validator, QuerySet, etc.

Django REST Framework includes built-in API browser for testing out newly developed API. Web API developed with Django REST Framework is web browsable, supports a wide range of media types, authentication and permission policies out of the box. API’s display data can use standard function based views, or granular with powerful class based views for more complex functionality.

Good Features of Django REST framework

  • Powerful Serialization that supports both ORM and non-ORM data sources.
  • Web browsable API is very useful for developers.
  • Authentication policies including OAuth1a and OAuth2 out of the box.
  • Easy to customize validators, parsers and authenticators.
  • Generic classes for CRUD operations.
  • Clean and simple views for Resources by using Django’s new class based views.
  • HTTP response handling, content type negotiation using HTTP Accept headers.
  • Pagination simplifies the process of returning paginated data in a way that can then be rendered to arbitrary media types.
  • Publishing of metadata along with querysets.

This is a brief introduction about Django REST Framework.

Hope you enjoy that.

By Asahi



Mac Studio

Apple has announced the latest desktop computer, MacStudio. This is a new company computer and the most powerful computer Apple has ever made. It’s twice the size of the Mac mini, but thanks to some great Mac Studio specs, it offers twice the performance of the latest MacBook Pro thanks to the new M1 Ultra processor.

Apple、まったく新しいMac StudioとStudio Displayを発表 - Apple (日本)
Credit: Apple

Inside your computer, Mac Studio specifications include Wi-Fi 6 and Bluetooth 5.0 for the fastest connection speeds. If you prefer a wired connection in your old school, Mac Studio will cover you too.

There are two USB-C ports on the front of Mac Studio. In the M1 Max version, these support USB 3, and in the M1 Ultra version, they are upgraded to Thunderbolt 4. There is also an SD card slot on the front, which is safe for photographers and videographers.

Credit: Apple

Inside is the M1 Ultra processor. It’s made from Apple’s latest Apple silicon processor and basically connects two M1 Max processors. The M1 Ultra has a 20-core CPU, a 48-core GPU 24, a 32-core neural engine 16, 64 GB of unified memory, and 1 TB of SSD storage. Debuting on the MacBook Pro in late 2021, the M1 Max accounts for half of all these numbers and still surprises everyone at launch.

This is my personal favourite for now as a work stations and I hope Apple will annouce a beast macbook pro by estimating with the products launched this time

Yuuma



Laravel 8のメール送信の例と、alwaysTo()を使って誤送信を防ぐ方法

今回は、Laravel 8のメール送信の例と、alwaysTo()を使って誤送信を防ぐ方法を紹介します。

Laravel 8には、メール送信のためのmailクラスがあり、メール送信のためのドライバーを複数用意し、お好みで使用することができます。smtp、Mailgun、Postmark、Amazon SES、sendmailが使用できます。どのドライバを使用するかは、envファイルで設定する必要があります。
今回はsmtpドライバを使用します。

.envでSMTPを設定する

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=Add your user name here

※メールを使い場合、googleアカウントのless secure app accessでturnをオンにする必要があります。

利用可能なメールクラスの作成

以下のコマンドを使用して、MyTestMailという名でメイラブルクラスを作成します。

php artisan make:mail MyTestMail

MyTestMailで以下のように変更する必要があり

    public $details;
    public function __construct($details)
    {
        $this->details = $details;
    }

    public function build()
    {
        return $this->subject('Mail from AMI')
                    ->view('emails.myTestMail');
    }

ブレードビューの作成

ブレードビューファイルを作成し、送信するメールを記述します。「emails」フォルダに以下のようなファイルを作成します。

<!DOCTYPE html>
<html>
<head>
    <title>メール送信テスト</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>Thank you</p>
</body>
</html>

最後に、テストメールを送信するための “MyTestMail “を作成します。


Route::get('send-mail', function () {
   
    $details = [
        'title' => 'Mail from AMI',
        'body' => 'This is for testing email using smtp'
    ];
   
    \Mail::to('name@gmail.com')->send(new \App\Mail\MyTestMail($details));
   
    dd("Email is Sent.");
});

プロジェクトを実行すると結果は

ステージング環境から実際のお客様に誤って何千通ものメールを送信してしまうこともあります。その時、どうのように設定するのかを共有します。

class AppServiceProvider extends ServiceProvider
{
    // Stuff omitted
 
    public function boot()
    {
        f (! app()->environment('production')) {
            Mail::alwaysTo('ex@example.org');
        }
    }
}

alwaysToで何が起きているのか?

alwaysTo() メソッドは、メールメッセージ内の to, cc, bcc に追加されたすべてのアドレスを上書きします。

そこで、上記のコードでは、Laravelに、本番環境でない場合のみ、ex@example.org にメールを送信するように指示しています。

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

By Ami



アプリ関連ニュース

お問い合わせはこちら

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

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

お問い合わせフォーム