0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OctoberCMSで日本語のFakerを使いたいとき

Last updated at Posted at 2021-06-14

やりたいことは表題の通り。
pagesの中で使いたい場合は適当なプラグインを作って、Plugin.phpの中のboot()からページへFakerのオブジェクトを渡してあげます。
具体的には、

composer require fzaninotto/faker

まずcomposerでfakerをインストールしたのち、

Plugin.php
use Faker\Factory;
use Event;

public function boot()
{
    Event::listen('cms.page.beforeDisplay', function($controller, $url, $page) {
        $controller->vars['faker'] = Factory::create('ja_JP');
    });
}

こうすれば、pagesの中からfakerという名前の変数としてFakerオブジェクトが呼び出せます。
あとは適当に、{{faker.name}}とかで呼べばいい感じのデータが得られると思います。その辺の詳しくはLaravel向けの資料を調べてみましょう。

また、Seederなどの中で呼び出したい場合も同様、Factory::create('ja_JP')でオブジェクトを作り、そこからいろいろ関数を呼べばデータが取り出せます。
OctoberCMS、Laravelベースで作ってくれてるのでこういうときに楽ですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?