#Multi Files Upload from products controller

1 messages · Page 1 of 1 (latest)

soft herald
#

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']);
    }
jolly jewel
soft herald
#

@jolly jewel how about this part

        $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,
            ]);
#

can i make like service that handels that for me

jolly jewel
soft herald
#

I am reading it know but i want to make a note in here that the Images is diffrent table

jolly jewel
#

And?

#

Still not how you upload images from a request.

soft herald
#

@jolly jewel i think what you mean that there ar no need for the move() store can do iit for me

jolly jewel
#

And if you’ve created a Product model, and that model has a has-many relation for your images model, then you can use that to create your images:

$product = Product::create($request->safe()->except('files'));

foreach ($request->file('files') as $file) {
    $product->images()->create([
        'path' => $file->store('product-images'),
        'size' => $file->size(),
    ]);
}
soft herald
#

yes i did it and i add that to my notes i will continue reading the docs about the part of the file uploads

#

thank you very much matrin really appreciate it

#

🙂

#

last questiion when i mark the post as solved can i get all of the records of my question to be like a refrence

#

i mean if i wan to come to this post after alike month from now can i get it or it will be archived o removed

jolly jewel