LoginSignup
mochikichi32
@mochikichi32 (mochikichi)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

戻るボタンで前の画面に戻ると違う値が取得されている

解決したいこと

laravelで編集機能を作成しています。

戻るボタンを使い、詳細画面に戻ると、
一覧画面の最後に表示されているデータが表示されてしまいます。

どこがおかしいのか分からず、困っています。
教えて頂きたいです。

edit.blade.php

@extends('layouts.layout')
@section('title','編集画面')
@section('content')
<div class="header">
    <h2>@section('main-title','商品編集フォーム')</h2>
</div>
<div>
    <form action="{{route('update', ['id'=>$product->id])}}" enctype="multipart/form-data" method="post">
    @csrf
        <div>
            <label for="">商品画像</label>
            <div>
                <input type="file" name="img_path" class="form-control-file">
            </div>
            <img src="{{ asset('storage/image/' . $product->img_path) }}" alt="$product->img_path" width="25%">
        </div>
        <div>
            <label for="">商品名</label>
            <div>
                <input 
                    id="product_name"
                    name="product_name"
                    value="{{ old('product_name') ?:$product->product_name }}"
                    type="text"
                >
            </div>
        </div>
        <div>
            <label for="">価格</label>
            <div>
                <input 
                    id="price"
                    name="price"
                    value="{{ old('price') ?:$product->price }}"
                    type="text"
                >
            </div>
        </div>
        <div>
            <label for="">在庫数</label>
            <div>
                <input 
                    id="stock"
                    name="stock"
                    value="{{ old('stock') ?:$product->stock }}"
                    type="text"
                >
            </div>
        </div>
        <div>
            <label for="">コメント</label>
            <div>
                <textarea 
                    name="comment" 
                    id="comment" 
                    cols="40" 
                    rows="5"
                >
                {{$product->comment}}
                </textarea>
            </div>
        </div>
        <div>
            <select name="company_name" id="company_name">
                @foreach ($products as $product)
                    <option value="">{{ $product->company_name }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <input type="submit" class="btn-success"></input>
            <a href='{{ route("detail", ["id"=> $product->id]) }}'>
                <input type="button" value="戻る" class="btn-dark">
            </a>
        </div>
    </form>
</div>
@endsection

ProductComtroller.php


    /** 
     * 編集画面
     *@param int $id
     * @return view
    */
    public function showEdit($id) {
        $products = Product::all();
        $product = Product::findOrFail($id);

        return view('edit',['products' => $products,'product' => $product]);
    }
    /**
     * 編集機能
     * @param int $id
     * 
     */
    public function update(Request $request, $id){
        //dd($request);
        $product = Product::find($id);

        $product->product_name = $request->input('product_name');
        $product->company_name = $request->input('company_name');
        $product->price = $request->input('price');
        $product->stock = $request->input('stock');
        $product->comment = $request->input('comment');

        $image = $request->file('img_path');
        $path = $product->img_path;
        if(isset($image)){
            \Storage::disk('public')->delete($path);
            $path = $image->store('public/image');
            $filename = basename($path);
            $product->img_path = $filename;
        }
        $product->save();
        return redirect(route('detail',['id' => $product->id]));
    }

###自分で試したこと
おかしい箇所はforeachのところっぽく、
そこを消すと、戻るボタンが正常に働きます。

ただ、ここまでしかわかりませんでした。

0

1Answer

@foreachで変数$productを上書きしています。
なので戻るボタンのidが変数$productsの最後の要素の値になっているのでしょう。

edit.blade.php
        <div>
            <select name="company_name" id="company_name">
                @foreach ($products as $product) 
                    <option value="">{{ $product->company_name }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <input type="submit" class="btn-success"></input>
            <a href='{{ route("detail", ["id"=> $product->id]) }}'>
                <input type="button" value="戻る" class="btn-dark">
            </a>
        </div>

シンプルにした例がこちらです。

$item = 'xxx';
$items = ['AAA', 'BBB', 'CCC'];

echo "$item\n";

foreach ($items as $key => $item) {
    echo "$key:$item\n";
}

echo "$item\n";

// xxx
// 0:AAA
// 1:BBB
// 2:CCC
// CCC

上書きしないように変数名を変更しましょう。

1

Comments

  1. @mochikichi32

    Questioner
    解決しました。
    ありがとうございます!

Your answer might help someone💌