新規登録画面で画像を登録したい

実現したいこと

画像付きの商品データ登録をしたい

前提

商品リストのシステムを作成しています。
新規登録画面が完成して、
画像の入力がない場合はデフォルトでno-imageの画像が表示されるようにしたかったので、モデルを編集してマイグレーションし直したのですが、その直後から
以下ののエラーが出るようになりました。
それまではエラーなく追加できていたので、何が原因でエラー出るようになったのかわかりません。

発生している問題・エラーメッセージ

Attempt to assign property "image_path" on null

イメージ説明

該当のソースコード

Larabel

1public function store(Request $request){ 2 3 $post=new Item(); 4 $post->product=$request->product; 5 $post->category_id=$request->brand_id; 6 $post->price=$request->price; 7 $post->stock=$request->stock; 8 if(isset($request->image_path)){ 9 $file_name=$request->image_path->getClientOriginalName(); 10 $item->image_path=$request->file('image_path')->storeAs('storage/images',$file_name); 11 } 12

Larabel

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('items', function (Blueprint $table) { 17 $table->id(); 18 $table->string('image_path')->default('/storage/images/no-Image.jpg'); 19 $table->string('product'); 20 $table->integer('price'); 21 $table->integer('stock'); 22 $table->foreignId('category_id')->constrained()->cascadeOnDelete(); 23 $table->string('comment'); 24 $table->timestamps(); 25 }); 26 } 27 28 /** 29 * Reverse the migrations. 30 * 31 * @return void 32 */ 33 public function down() 34 { 35 Schema::dropIfExists('items'); 36 } 37}; 38

![

Larabel

1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <title>Commodity</title> 6 <link rel="stylesheet" href="../../../public/css/style.css" type="text/css"> 7</head> 8<body> 9 <div class="container"> 10 <div class="new-contents"> 11 <h1>商品新規登録画面</h1> 12 <div class="form_frame"> 13 <!-- <form method="post" action="../store"> --> 14 <form method="post" action="{{route('post.store')}}" enctype="multipart/form-data"> 15 @csrf 16 <ul> 17 <li> 18 <div> 19 <label for="product">商品名<span>*</span></label> 20 <input type="text" id="product" name="product"> 21 </div> 22 </li> 23 <li> 24 <div> 25 <label for="brand">メーカー名<span>*</span></label> 26 <select id="brand" name="brand_id"> 27 @foreach ($categories as $category) 28 <option value="{{$category->id}}">{{$category->brand}}</option> 29 @endforeach 30 </select> 31 </div> 32 </li> 33 <li> 34 <div> 35 <label for="price">価格<span>*</span></label> 36 <input type="number" id="price" name="price"> 37 </div> 38 </li> 39 <li> 40 <div> 41 <label for="stock">在庫<span>*</span></label> 42 <input type="number" id="stock" name="stock"> 43 </div> 44 </li> 45 46 <!-- <li> 47 <div> 48 <label for="comment">コメント</label> 49 <input type="text" id="comment"> 50 </div> 51 </li> --> 52 <li> 53 <div> 54 <label for="pic">商品画像</label> 55 <input type="file" id="pic" name="image_path"> 56 </div> 57 </li> 58 <li> 59 <div> 60 <label for="comment">コメント</label> 61 <textarea id="comment" name="comment"></textarea> 62 <!-- <input type="text" id="pic" name="pic"> --> 63 </div> 64 </li> 65 </ul> 66 <div class="btn-section"> 67 <button class="entry-btn">新規登録</button> 68 69 <button type="button" onclick="history.back()" class="return-btn">戻る</button> 70 </div> 71 </form> 72 </div> 73 74 </div> 75 </div> 76</body> 77</html> 78 79

試したこと

関係するファイルを見直して、インターネットでも調べましたが解決できませんでした。

補足情報(FW/ツールのバージョンなど)

Laravel Framework 9.52.16

コメントを投稿

0 コメント