実現したいこと
画像付きの商品データ登録をしたい
前提
商品リストのシステムを作成しています。
新規登録画面が完成して、
画像の入力がない場合はデフォルトで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

0 コメント