update: 画像アップロード用のカラム(main_image)を追加#31
Open
cane-bt wants to merge 3 commits into
Open
Conversation
cane-bt
commented
May 23, 2021
| $recipe->description = $request->description; | ||
| $recipe->is_comment_allowed = $request->is_comment_allowed; | ||
| $recipe->is_published = $request->is_published; | ||
| $recipe->main_image = basename($request->main_image->storeAs('public/main_images', date("YmdHis") . '.jpg')); |
Owner
Author
There was a problem hiding this comment.
以下を参考にmain_imageに値を設定しています。
そもそもstoreアクションで値の受け渡しなどができていないのでこのコードは意味が内容にも感じます。
laravelで画像アップロードの覚え書 | むちむちコード
cane-bt
commented
May 23, 2021
| <div class="form-group"> | ||
| <div class="custom-file"> | ||
| <input type="file" class="custome-file-input" id="inputFile"> | ||
| <input type="file" name="main_image" class="custome-file-input" id="inputFile"> |
Owner
Author
There was a problem hiding this comment.
controller側で$requestに格納できるようにname属性を追加。
ただし、laravel.logで確認するとmain_imageのデータが渡せていないようにみえます。
[2021-05-23 23:50:02] local.DEBUG: GET {"path":"/","query":""}
[2021-05-23 23:51:15] local.DEBUG: POST {"path":"/","query":"_token=EgsVC34G7QD1IPCJT4yx7DDzoCRO2piq71yLnK43&name=sdfgh&cookingtime=12&description=dfg&tool%5B0%5D=1&is_comment_allowed=0&is_published=0"} 原因はname属性の有無以外にあるのでしょうか?
cane-bt
commented
May 23, 2021
| // 'is_published' => $request->boolean('is_published') ? 1 : 0 | ||
| // ]); | ||
|
|
||
| dd($request->main_image); |
Owner
Author
There was a problem hiding this comment.
ddコマンドでブラウザ側で変数の値を確認したいのですができません。
ddコマンドが機能するときと機能しないときの違いはなんでしょうか?
cane-bt
commented
May 24, 2021
| <div class="col-md-8 col-md-offset-1"> | ||
| @include('recipe/message') | ||
| <form action="/recipe" method="post"> | ||
| <form action="/recipe" method="post" enctype="multipart/form-data"> |
Owner
Author
There was a problem hiding this comment.
以下を参考に追加してみました。
Laravelでファイルをアップロードする方法を詳細解説 | アールエフェクト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
困っていること
フォームで画像のアップロードを行おうとするがエラーが下記のエラーメッセージが表示されて保存ができない状態です。

main_imageカラムはmigrationであとから追加しました。laravel.logファイルでログを見てみると追加したmain_imageカラムがパラメーターとして送られていません。[2021-05-23 15:31:41] local.DEBUG: POST {"path":"/","query":"_token=yfOR2Ky0N0gAoLKjdwoNvTUwdBAhfMLQWUgc6Egl&name=asd&cookingtime=12&description=df&tool%5B0%5D=2&is_comment_allowed=1&is_published=1"} [2021-05-23 15:31:42] local.DEBUG: GET {"path":"/","query":""}ただしエラーメッセージをみる限りは、main_imageカラムのフォームに値が入れられている扱いになっているようにみうけられます。
実現したいこと、知りたいこと
実現したいことは
main_imageカラムの値を渡したいの2点です。
進めた手順
まずは以下のリンクをベースに進める。
laravelで画像アップロードの覚え書 | むちむちコード
だいたい内容が理解できたら以下を読めばある程度理解できな以下を参考に修正。
Laravelでファイルをアップロードする方法を詳細解説 | アールエフェクト
以下はかなり細かく設定がしてあるので参考になりそう。
Laravelで画像ファイルアップロードをする簡単なサンプル - Qiita