Как получить идентификатор продукта из скрытого поля Laravel - PullRequest
0 голосов
/ 30 июня 2019

Я пытаюсь получить Id продукта, когда я отправляю форму, используя скрытое поле ввода, но получаю ошибку Trying to get property of non-object. Как я могу исправить эту проблему?

код

Контроллер

class productController extends Controller
{

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(Request $request)
{
    $userId = $request->user()->id;
    $products = product::where('admin', $userId)->get();
   return view('admin.product.index',compact('products'));
}



/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */

public function admin()
{
   $products=product::all();
   return view('admin.product.index',compact('products'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request) 
{ 

    $formInput=$request->all();
    $image=array();
    if($files=$request->file('image')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('images',$name);
            $image[]=$name;

        }
       // dd($formInput);

    }

    //dd($formInput); 
    Image::create(array_merge($formInput,[
       // $id=$request->input('id'), 
       // $product=Product::find($id),
        $request->input('product_id'),
    ])); 
    return redirect()->back(); 

}

лезвие

   <form action="{{route('product.store')}}" method="post" 
   role="form" 
   enctype="multipart/form-data">
            {{csrf_field()}}
  @foreach ( $products as $product )
      <input type="hidden" name="product_id" value="{{ $product->id 
   }}" />
    @endforeach
    </form>

Любая помощь будет оценена.

1 Ответ

0 голосов
/ 30 июня 2019

Похоже, у вас есть более 1 product_id скрытое поле.

Если вы планируете отправить более 1, добавьте квадратные скобки к имени product_id[].

Объект запроса должен получить то же имя.Вы используете id в контроллере, но отправляете product_id именованное поле.

Используйте $request->input('product_id')

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...