mardi 15 octobre 2019

Upload multiple image with id and store to folder

I want to store images with id to table and save them to folder.I have two tables catalogs and images.Images table has img_id which is foreign key to id in catalogs table, code in my controller is:



        $catalog = new Katalog();
        $catalog->name = $name;
        $catalog->picturePath = $name . '/';
        $catalog->description = $desc;
        $catalog->address = $request->input('address');
        $catalog->phone_number = $request->input('phone_number');
        $catalog->email = $request->input('e_mail');
        $catalog->info_holder = $request->input('type');
        $catalog->partner_logo = $logo_img_name;
        $catalog->short_news = $request->input('short_news');
        $catalog->updated_at = Carbon::now();
        $catalog->site = $request->input('site');
        $catalog->creator_id = Auth::user()->id;
        $catalog->save();

        $id = Katalog::where('name', $name)->first()->id;


        if( $request->hasFile('images') ) {
            foreach ( $request->file('images') as $image ) {
                $img_name = $image->getClientOriginalName();
                $request->image->storeAs(('/images/' . $name), $img_name, 'images');

                Image::creat([
                    'img_id' => $id,
                    'img_name' => $img_name
                ]);
            }
        }


        return redirect('/myadmin/posts')->with('success', 'Статья создан!');

After storing data to catalogs table it returns new id and I want store images to images table with returned id but can not do this.Help me please thanks in advance.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2MLIaPw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire