Я пытаюсь получить 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>
Любая помощь будет оценена.