blackpearl's avatar

Join 2 method, upload images

Hello i would like to ask how join my 2 method in one controller ?

I have one method that adds a new news with photo and second that adds gallery

First method that adds news

public function store(PostFormRequest $request)
    {  
       if( $request->hasFile('image') ) {
        $file = $request->file('image');
        // Get the Image Name
        $fileName = $file->getClientOriginalName();
        // Set the Filepath
         $path = 'uploads';
        // Move the file to the upload Folder
        $file = $file->move($path, $fileName);
    }
        $post= new Post(array(
            'title' => $request->get('title'),
            'content' => $request->get('content'),
            'image' => $path.'/'.$fileName,
            'slug' => Str::slug($request->get('title'),'-'),
 
 
        ));
        $post->save();
        $post->categories()->sync($request->get('categories'));
 
        return redirect('/admin/posts/create')->with('status', 'The post has been created');
    }

Second method that adds images

public function doExample()
    {
        $product = Product::create([
            'title'=>Input::get('title'),
            'description' =>Input::get('description')
        ]);

        $images = Input::file('images');

        foreach($images as $image):
            $move = $image->move('public/images', $image->getClientOriginalName());

            if($move)
            {
                $imagedata = Image::create([
                    'title'=> $image->getClientOriginalName(),
                    'filename' => $image->getClientOriginalName()
                ]);

                $product->images()->attach([$imagedata->id]);
            }
        endforeach;

        dd($images);
    }

I want to add news and gallery in one controller

0 likes
1 reply
monsterdream's avatar

You can just add $this->doExample(); in your store() method. Of course with proper arguments.

Please or to participate in this conversation.