cpt_pall's avatar

Update method

Hello, i try write my update method to upload article with photo and gallery images but i don't know how. My store method works but with update method i have a problem

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);
    }
0 likes
4 replies
phildawson's avatar

@cpt_pall Also on another look, note that the move() method on the UploadedFile object doesn't return a boolean on success/fail.

     * @return File A File object representing the new file
     *
     * @throws FileException if, for any reason, the file could not have been moved

If it fails to move it'll throw an exception so if you want to catch them and say redirect or do nothing you'll want to modify that like so:

use Symfony\Component\HttpFoundation\File\Exception\FileException;
try {
    $photo->move(public_path('images'), $photo->getClientOriginalName());

    // .. Photo create

} catch(FileException $e) {

    // "Unable to write in the ../public/images directory"

}
cpt_pall's avatar

@phildawson my stupid mistakes with this $article->photos()->attach([$imagedata->id]); but when i try edit main- photo i get : Call to a member function move() on null

when i don't change main-photo then works but i would like to ask how load this images on edit view and for example delete 2 images and add another 3 and later update this image, not create new.

phildawson's avatar

@cpt_pall I personally separate the tasks out. You make the article, then you manage the photos for the article.

Route::resource('articles', 'ArticleController');
Route::resource('articles.photos', 'ArticlePhotoController');

Either that or I use a filemanger which ajaxs the uploads / picks from a gallery and returns the photo id to add to a hidden field then all you'll be doing is

$ids = $request->photos; // [1,2,3]
$article->photos()->sync($ids);

A few other tips

$article = Article::whereId($id)->firstOrFail();

=

$article = Article::findOrFail($id);

and

$article->title = $request->get('title');

=

$article->title = $request->title;

and

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

=

$photos = $request->file('images');

I would also use the constructor to inject the article dependency so your request ends up like so:

public function update($id, RequestForm $request)
{
    $model = $this->model->findOrFail($id);
    $model->update($request->all());
    $model->photos()->sync($request->photos);
}

or even just

public function update($id, RequestForm $request)
{
    $this->model->findOrFail($id)->update($request->all());
}

and move the sync to a trait such a PhotographableTrait which adds the photos() relationship and does the sync when the model is saved.

If you find a method is doing more than one thing its time to extract and refactor, ideally it should only ever be a few lines at most.

Please or to participate in this conversation.