2
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 1 year has passed since last update.

Laravel UI登録画面で画像保存処理を実装

Last updated at Posted at 2022-06-23

概要

Laravel UI導入後に登録画面に画像保存処理を実装したので記録します。
スクリーンショット 2022-06-23 23.59.17.png

  1. バージョン
  2. View
  3. Controller

バージョン

Laravel Framework 9.6.0
PHP 8.1.0
Laravel UI 8.5.19
bootstrap導入済み
シンボリックリンク作成済み

View

formタグにenctype="multipart/form-data"を追記

redister.blade.php
...省略...
<div class="card-body">
    <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
    @csrf
    {{-- アイコン画像 --}}
    <div class="row mb-3">
        <label for="icon" class="col-md-4 col-form-label text-md-end">{{ __('アイコン') }}</label>
        <div class="col-md-6">
            <input id="icon" type="file" class="form-control" name="icon_image_path">
        </div>
    </div>
...省略...

Controller

icon_image_pathがあればstorage/app/public/imgにそのまま保存させ、なければnullとする。

RegisterController.php
...省略...
          /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        if (isset($data['icon_image_path'])) {
            $icon_image_path = $data['icon_image_path']->store('img', 'public');
        } else {
            $icon_image_path = null;
        }

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'icon_image_path' => $icon_image_path // Usersテーブルにicon_image_pathカラム追加済み
            ...省略...
        ]);
    }
2
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
2
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?