hello i have a controller to store a products data to the products model and store the images to that product to the Pimages Model i Implement that in the store method as below in this code i instantiated a product model and the Pimage model but as you can see the code is to big is there is a way to make it smaller spically in the part of model initialization
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required',
'description' => 'required',
'category' => [
'required',
Rule::in(['1', '2'])
],
'slug' => 'required',
'files' => 'required',
'files.*' => 'mimes:png|max:2048'
]);
$product = Product::create([
'name' => $request->get('name'),
'description' => $request->get('description'),
'category_id' => $request->get('category'),
'slug' => $request->get('slug'),
]);
foreach ($request->file('files') as $value) {
$path = $value->store();
$moveFiles = $value->move(public_path('Uploads'), $path);
$Images = Pimage::create([
'path' => $moveFiles->getRealPath(),
'size' => $moveFiles->getSize(),
'product_id' => $product->id,
]);
}
return view('products.create',['title'=>'Create','message'=>'Products Added']);
}