cpt_pall's avatar

images/gallery

I try write a method which a create a new Article with title,content,main photo and gallery but my method doesn't work

 public function doNews(Request $request)
    {
        if( $request->hasFile('photo') ) {
        $file = $request->file('photo');

        $fileName = $file->getClientOriginalName();

        $path = 'uploads';

        $file = $file->move($path, $fileName);
        }

        $articles = new Article(array(
            'title' => $request->get('title'),
            'description' => $request->get('description'),
            'photo' => $path.'/'.$fileName,
        ));

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

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

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

                $articles->photos()->attach([$imagedata->id]);
            }
        endforeach;

        dd($images);
    }
0 likes
2 replies
christopher's avatar

I love these sentences "does not work"!

What does not work? Are you getting an error message? Which error message?

1 like
cpt_pall's avatar

I fixed my method and work's

public function doNews(Request $request)
    {
        if( $request->hasFile('photo') ) {
        $file = $request->file('photo');

        $fileName = $file->getClientOriginalName();

        $path = 'uploads';

        $file = $file->move($path, $fileName);
        }

        $articles = new Article(array(
            'title' => $request->get('title'),
            'description' => $request->get('description'),
            'image' => $path.'/'.$fileName,
        ));
        $articles->save();
        $photos = Input::file('images');

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

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

                $articles->photos()->attach([$imagedata->id]);
            }
        endforeach;

        dd($articles);
    }

but i have problem with my update method , i don't know how write her

public function update(Request $request, $id)
    {
        if( $request->hasFile('photo') ) {
        $file = $request->file('photo');

        $fileName = $file->getClientOriginalName();

        $path = 'uploads';

        $file = $file->move($path, $fileName);
        }
        $article = Article::whereId($id)->firstOrFail();
        $article->title = $request->get('title');
        $article->description = $request->get('description');
        $article->image = $request->get('photo');
        $article->save();

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

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

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

                $articles->photos()->attach([$imagedata->id]);
            }
        endforeach;

        dd($articles);
    }

Can someone help me?

Please or to participate in this conversation.