Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LaraCSVで手軽にCSVを出力する/Easily generate CSV by LaraCSV

kkznch
May 16, 2019

LaraCSVで手軽にCSVを出力する/Easily generate CSV by LaraCSV

Laravel Meetup宮崎
登壇枠で使用

参考URL
https://re-build.connpass.com/event/129466/

kkznch

May 16, 2019
Tweet

More Decks by kkznch

Other Decks in Technology

Transcript

  1. LaraCSVͰ
    खܰʹCSVΛग़ྗ͢Δ
    גࣜձࣾRe:Build
    Յ਺ ါىʢ@kkznchʣ
    2019೥5݄16೔ʢ໦ʣ
    Laravel Meetup Miyazaki

    View full-size slide

  2. ͚͚ͣΜ @kkznch
    Յ਺ ါى
    1990೥ͷԭೄੜ·Ε
    ిྗձࣾͷITΠϯϑϥɺηΩϡϦςΟ

    גࣜձࣾRe:BuildͰWebΤϯδχΞ

    View full-size slide

  3. ձࣾ঺հ
    • גࣜձࣾRe:Buildʢ2017೥11݄ʹઃཱʣ
    • ݱࡏͷࣾһ਺͸໊̑
    • ԭೄΛڌ఺ͱ͠ɺجຊతʹ͸Laravel΍Vue.jsΛ
    ࢖ͬͯडୗɾࣗࣾαʔϏε։ൃΛ͍ͯ͠Δ
    • ϦϞʔτϫʔΫɺࣗ༝ͳಇ͖ํΛਪ঑͍ͯ͠Δ

    View full-size slide

  4. ϥΠϒϥϦΛ௥Ճ͢Δ
    # terminal͔ΒԼهͷίϚϯυΛ࣮ߦ͢Δ
    $ composer require usmanhalalit/laracsv:^2.0

    View full-size slide

  5. CSVͷϏϧυ
    // ΠϯελϯεΛੜ੒͢Δ
    $csvExporter = new \Laracsv\Export();
    // ୈҰҾ਺ʹCollectionΛɺ
    // ୈೋҾ਺ʹCSVͰग़ྗ͍ͨ͠ϞσϧͷΧϥϜ໊Λೖྗ͢Δ
    $csvExporter->build(Users::get(), ['email', 'name']);

    View full-size slide

  6. CSVͷμ΢ϯϩʔυ
    // μ΢ϯϩʔυ͢Δ
    $csvExporter->download();
    email,name
    [email protected],"Ԭ෦ ྙଠ࿠"
    [email protected],"௣໊ ·ΏΓ"
    [email protected],"ڮా ࢸ"
    // ϑΝΠϧ໊Λࢦఆͯ͠μ΢ϯϩʔυ͢Δ
    $csvExporter->download('users.csv');

    View full-size slide

  7. ྑ͍ͱࢥͬͨͱ͜Ζ

    View full-size slide

  8. ϦϨʔγϣϯ΋औಘ͢Δ
    // Ϟσϧʹఆٛ͞Ε͍ͯΔϦϨʔγϣϯΛࢦఆ͢Δ͚ͩ
    $csvExporter->build(Users::get(),
    ['email', 'name', ‘department.name’]);

    View full-size slide

  9. ஋ͷ௥Ճɾมߋ͕Ͱ͖Δ
    $csvExporter->beforeEach(function($user) {
    // طଘͷ஋Λ্ॻ͖Ͱ͖Δ
    $user->email = '[email protected]';
    // ଘࡏ͠ͳ͍஋Λ௥ՃͰ͖Δ
    $user->note = $user->name . 'ϝϞͰ͢';
    });
    $csvExporter->build(User::get(),
    [‘email', 'name', ‘note'])->download();
    email,name,note
    [email protected],"Ԭ෦ ྙଠ࿠","Ԭ෦ ྙଠ࿠ϝϞͰ͢"
    [email protected],"௣໊ ·ΏΓ","௣໊ ·ΏΓϝϞͰ͢"

    View full-size slide

  10. ࣮຿Ͱͷ࢖͍ॴ͸ʁ

    View full-size slide

  11. ࢖͍ॴ
    • τϥϯβΫγϣϯσʔλͷCSVग़ྗ
    • ܭࢉ݁ՌΛ֨ೲͨ͠࿈૝഑ྻͷCSVग़ྗ
    ‣ ഑ྻΛCollectionܕʹม׵ͯ͠౉͢ͱಈ͘

    View full-size slide

  12. CSVϥΠϒϥϦͷൺֱ

    View full-size slide

  13. CSVϥΠϒϥϦ
    ͍Ζ͍Ζ͋Δ

    View full-size slide

  14. Packalyst ʢCSVϥΠϒϥϦ DL਺߱ॱʣ

    View full-size slide

  15. ৮Γͭͭ଎౓Λൺֱͯ͠Έͨ

    View full-size slide

  16. ൺֱର৅
    • laracsv
    • excel
    • laravel-formatter
    • fast-excel

    View full-size slide

  17. ࣮ݧ
    • string(255)ܕΧϥϜΛ10ݸ࣋ͭςʔϒϧ
    • Ϩίʔυ਺͸5120݅
    • ֤ϥΠϒϥϦͰCSVϑΝΠϧΛग़ྗ͢Δ·Ͱ
    ͷ࣌ؒΛଌఆ͢Δ

    View full-size slide

  18. ݁Ռ
    ࣌ؒʢඵʣ ίϝϯτ
    MBSBDTW ଎౓͸஗͍͕࢖͍উख͸Α͍
    FYDFM
    ग़ྗ͢ΔͨΊʹ&YQPSU༻ΫϥεΛ
    ༻ҙ͢Δඞཁ͕͋ͬͯ໘౗͍͘͞
    MBSBWFMGPSNBUUFS
    ϑΥʔϚοτͷม׵͚ͩߦΘΕΔɻ
    ϑΝΠϧ΁ͷॻ͖ग़͠͸ཁࣗ࡞
    GBTUFYDFM
    ଌఆϛεͬͨͷ͔ͳʜ
    ϫϯϥΠφʔͰ$47ര଎ग़ྗ͸૲

    View full-size slide

  19. ·ͱΊ
    • LaraCSVͰखܰʹCSVͷग़ྗ͕Ͱ͖Δ
    ‣ ग़ྗ͢ΔΧϥϜͷࢦఆ͕༰қ
    ‣ ϦϨʔγϣϯઌͷ஋΋ࢦఆͰ͖Δ
    • ଎͞Λ௥ٻ͢Δ͋ͳͨ͸fast-excelΛͲ͏ͧ

    View full-size slide