samedi 24 octobre 2015

Laravel 5: Uploaded image not being attached to article_id

Trying to upload an image when creating an article and have that image be attached to the article so I can do something like {{ $article->image->path }} in my view. So far, everything is set up good and the article gets saved to the correct directory and the path gets added to the database (although the path is strange, for example a file named "moon-wallpaper" will be saved as "img/moon-wallpaper.jpg.jpg" -- it adds the extension twice for some reason). But anyways..

The problem in my question is that the image path that was saved to the images table should have an article_id attached to it to be referenced in the view... but I get an error and the error says "trying to get property of non object" because $response->id in the store method evaluates to null =S what am I doing wrong? How can I do this correctly?

Any help would be great!

store method in Articles Controller:

public function store(ArticleRequest $request)
    {
        $this->createArticle($request);

        $image = $request->file('image');

        $uploads = 'img/';

        $imageName = $image->getClientOriginalName().'.'.$image->getClientOriginalExtension();

        $image->move($uploads, $imageName);

        $response =  $this->createArticle($request);

        Image::create([
            'path' => $uploads.$imageName,
            'article_id' => $response->id
        ]);

        return redirect('blog');
    }

using a private method to create the article. createArticle method:

private function createArticle(ArticleRequest $request)
    {
        $article = Auth::user()->articles()->create($request->all());

        $this->syncTags($article, $request->input('tag_list'));
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/205w8RS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire