pom's avatar
Level 21

ProjectFlyer Lesson 16 Problem

I've spent practically all day trying to nut this one out, I need some help. I've finished lesson 16 and when I try to upload an image to a flyer I get the error 'Call to a member function addPhoto() on null'.

My PhotosController.php

public function store($zip, $street, ChangeFlyerRequest $request)
    {
        $flyer = Flyer::locatedAt($zip, $street);
        $photo = $request->file('photo');

        (new AddPhotoToFlyer($flyer, $photo))->save();
    }

I've dumped both the $flyer and the $photo and get expected results. However, both the $flyer and $photo become null after creating the AddPhotoToFlyer object.

My save function inside the AddPhotoToFlyer.php

public function save()
    {
        $photo = $this->flyer->addPhoto($this->makePhoto()); 
        $this->file->move($photo->baseDir(), $photo->name);
        $this->thumbnail->make($photo->path, $photo->thumbnail_path);
    }

Any help would be greatly appreciated.

0 likes
12 replies
lindstrom's avatar

I compared to mine and your methods are identical so the issue is somewhere else. Are you returning in you addPhoto method in your Flyer model?

    public function addPhoto(Photo $photo)
    {
        return $this->photos()->save($photo); // instead of $this->photos()->save($photo);
    }
1 like
pom's avatar
Level 21

Yes, I have the same as you.

public function addPhoto(Photo $photo)
    {
        return $this->photos()->save($photo);
    }

public function photos()
    {
        return $this->hasMany('App\Photo');
    }

It's got me stumped.

ayekoto's avatar

Show your makePhoto() method , are you sure you return it?

1 like
pom's avatar
Level 21
protected function makePhoto()
    {
        return new Photo(['name' => $this->makeFileName()]);
    }

    protected function makeFileName()
    {
        $name = sha1(time() . $this->file->getClientOriginalName());

        $extension = $this->file->getClientOriginalExtension();

        return "{$name}.{$extension}";
    }
ayekoto's avatar

Here's okay, I suspect you should check your add AddPhotoToProject constructor, and be sure you properly inject them,

  • check if your properly import the class... I did the project flyer n work well ..

Must have been a little mistake somewhere

pom's avatar
Level 21

Here it is in it's entirety

<?php

namespace App;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class AddPhotoToFlyer
{
    protected $flyer;
    protected $file;

    public function __contruct(Flyer $flyer, UploadedFile $file, Thumbnail $thumbnail = null)
    {
        $this->flyer = $flyer;
        $this->file = $file;
        $this->thumbnail = $thumbnail ?: new Thumbnail;
    }

    public function save()
    {
        $photo = $this->flyer->addPhoto($this->makePhoto()); //BROKEN!!
        $this->file->move($photo->baseDir(), $photo->name);
        $this->thumbnail->make($photo->path, $photo->thumbnail_path);
    }

    protected function makePhoto()
    {
        return new Photo(['name' => $this->makeFileName()]);
    }

    protected function makeFileName()
    {
        $name = sha1(time() . $this->file->getClientOriginalName());

        $extension = $this->file->getClientOriginalExtension();

        return "{$name}.{$extension}";
    }
}
ayekoto's avatar

Okay, Include @the top of the page

use App\Flyer;
use App\Photo;
//also include your thumbnail_path
ayekoto's avatar

Well, even without the App\Photo; I guess it should work...

Can't figure out what's wrong... But try to include those at the top n check

pom's avatar
Level 21

What do you mean by //also include your thumbnail_path

joedawson's avatar
Level 18

@pom you have a typo in your _construct method. You've missed the "s"

2 likes
pom's avatar
Level 21

@JoeDawson you're a legend, thank you. I knew it was going to be something simple that I couldn't see for looking.

Please or to participate in this conversation.