前提・実現したいこと
1枚の画像アップロードはできるのですが、複数画像をアップロードしようとすると配列をどのように扱ってよいかわからず実装できず困っています。
create.blade.phpの画像nameはimage1~4で実装しています。image[]にしている参考サイトが多く色々試してみたのですがうまく実装できず今の現状へ至っています。
ImagesControllerのisset($form[ここにどうやって配列を持ってくればよいか])
$path = $request->file(ここをどうやって配列を持ってくればよいか)->store('public/image');
がわかりません。
他にもこのままでは実装できない原因があればアドバイスを頂きたいです。
よろしくお願い致しますm(__)m
itesテーブル
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateItemsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('items', function (Blueprint $table) { $table->bigIncrements('id'); //主キー $table->string('item_name', 100); $table->string('explanation', 255)->default(''); $table->integer('amount'); //amout=金額 $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() //関数downには、マイグレーションの取り消しを行う為のコードを書きます { Schema::dropIfExists('items');//ここでは、もしitemsというテーブルが存在すれば削除すると書かれています } }
item_photosテーブル
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateItemPhotosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('item_photos', function (Blueprint $table) { $table->Bigincrements('id'); $table->Biginteger('item_id')->unsigned(); //外部キー属性の時よく使う、符号なしに設定 $table->foreign('item_id')->references('id')->on('items'); //外部キー参照 $table->string('image_path')->nullable()->default(''); //->nullable()という記述は、画像のパスは空でも保存できます、という意味 $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('item_photos'); } }
Itemモデル
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Item extends Model { protected $fillable = [ 'item_name', 'explanation', 'amount', 'inventory_control', ]; public static $rules = array( 'item_name' => 'required', 'explanation' => 'required', 'amount' => 'required', 'inventory_control' => 'required' ); public function photos() { return $this->hasMany('App\ItemPhoto'); } }
ItemPhotoモデル
<?php namespace App; use Illuminate\Database\Eloquent\Model; class ItemPhoto extends Model { protected $fillable = ['item_id', 'image_path']; public static $rules = array( 'image_path' => 'required', ); public function item() { return $this->belongsTo('App\Item'); } }
ImagesController
<?php namespace App\Http\Controllers\Admin; use Symfony\Component\HttpFoundation\Response; use Illuminate\Http\Request; use App\Item; use Storage; use Carbon\carbon; use App\ImagesHistory; use App\Http\Controllers\Controller; use App\Admin; use \InterventionImage; use App\ItemPhoto; class ImagesController extends Controller { public function create(Request $request) { $this->validate($request, Item::$rules); //Itemモデルでバリデーションした(item_name,explanation,amount,inventory_control,item_pic_path) // $this->validate($request, ItemPhoto::$rules); $form = $request->all(); //$request->all()は全入力を連想配列で取得 $item = new Item(); //新規Itemインスタンス作成 // フォームから送信されてきた_tokenを削除する、一時保存を削除すると考えてよい? unset($form['_token']); $item->fill($form)->save(); //先にitemのレコードがないと子であるitem_photosは入れられないため、データベースへ保存 $item_photos = array(); //空の配列用意 $form[] = 'image1'; //list.blade.phpのnameから取得して$form配列に入れる $form[] = 'image2'; $form[] = 'image3'; $form[] = 'image4'; if(isset($form['image1'])){ //issetは変数に値がセットされているか確認する方法、つまり$file_nameに'image_path'があるか確認している $path = $request->file('image1')->store('public/image'); //file()内にview側の画像のnameを書く、->store()で画像をpublic/storage/image保存 $item->image_path = basename($path);//basenameはパスの/以降の文字列を全て取得 $files = $request->file('image_path'); //file('読み込みたい変数、カラムなど文字列ならok') $item_photo = new ItemPhoto(); //ItemPhotoインスタンス作成 $item_photo->fill(["image_path"=>$form]); $item_photos[] = $item_photo; //各item_photoを用意した$item_photosに代入する InterventionImage::make($files)->resize(100, 100)->save(public_path()."/image/resize_image/". $item_photo->image_path); } //App::make()取得できる // フォームから送信されてきたimageを削除する unset($form['image_path']); if (!empty($item_photos)) { $item->photos()->saveMany($item_photos);//photos()の所は親(Itemモデル)のphotos()アクションでリレーションしているためphotos()を使う } return redirect('admin/images/create')->with('flash_message', '商品を追加しました'); }
create.blade.php
<form method="post" action="{{ route('item_regi') }}" enctype="multipart/form-data"> <div class="form-group row mt-5"> <label class="col-md-2">画像1</label> <div class="col-md-10"> <input class="form-control-file" type="file" name="image1" multiple="multiple"> </div> </div> <div class="form-group row mt-5"> <label class="col-md-2">画像2</label> <div class="col-md-10"> <input class="form-control-file" type="file" name="image2"> </div> </div> <div class="form-group row mt-5"> <label class="col-md-2">画像3</label> <div class="col-md-10"> <input class="form-control-file" type="file" name="image3"> </div> </div> <div class="form-group row mt-5"> <label class="col-md-2">画像4</label> <div class="col-md-10"> <input class="form-control-file" type="file" name="image4"> </div> </div> {{ csrf_field() }} <input type="submit" class="btn btn-primary mt-3" value="商品登録"> </form>
list.blade.php
@foreach($items as $item) <tr> <th>{{ $item->id }}</th> <td>{{ Str::limit($item->item_name, 10) }}</td> <td>{{ Str::limit($item->amount, 5) }}円</td> <td>{{ Str::limit($item->explanation, 200) }}</td> <td>{{ Str::limit($item->inventory_control, 5) }}</td> @foreach($item->photos as $photo) <td> <a href="/items/{{ $item->id }}"><img src="{{ asset('/image/resize_image/' . $photo->image_path) }}" alt=""></a> </td> @endforeach </tr> @endforeach
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。