Skip to content

update: 画像アップロード用のカラム(main_image)を追加#31

Open
cane-bt wants to merge 3 commits into
mainfrom
feature/add_function_upload_image
Open

update: 画像アップロード用のカラム(main_image)を追加#31
cane-bt wants to merge 3 commits into
mainfrom
feature/add_function_upload_image

Conversation

@cane-bt
Copy link
Copy Markdown
Owner

@cane-bt cane-bt commented May 23, 2021

困っていること

フォームで画像のアップロードを行おうとするがエラーが下記のエラーメッセージが表示されて保存ができない状態です。
00_35_56

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カラムのフォームに値が入れられている扱いになっているようにみうけられます。

実現したいこと、知りたいこと

実現したいことは

  1. storeアクションにmain_imageカラムの値を渡したい
  2. DBに登録が完了してindexアクションで画像が表示できるようにしたい

の2点です。

進めた手順

まずは以下のリンクをベースに進める。
laravelで画像アップロードの覚え書 | むちむちコード

だいたい内容が理解できたら以下を読めばある程度理解できな以下を参考に修正。
Laravelでファイルをアップロードする方法を詳細解説 | アールエフェクト

以下はかなり細かく設定がしてあるので参考になりそう。
Laravelで画像ファイルアップロードをする簡単なサンプル - Qiita

@cane-bt cane-bt self-assigned this 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'));
Copy link
Copy Markdown
Owner Author

@cane-bt cane-bt May 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下を参考にmain_imageに値を設定しています。
そもそもstoreアクションで値の受け渡しなどができていないのでこのコードは意味が内容にも感じます。
laravelで画像アップロードの覚え書 | むちむちコード

<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">
Copy link
Copy Markdown
Owner Author

@cane-bt cane-bt May 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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属性の有無以外にあるのでしょうか?

// 'is_published' => $request->boolean('is_published') ? 1 : 0
// ]);

dd($request->main_image);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ddコマンドでブラウザ側で変数の値を確認したいのですができません。
ddコマンドが機能するときと機能しないときの違いはなんでしょうか?

<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">
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant