sergiuwd's avatar

Call to a member function on null

Hello! I am learning Laravel following the tutorials. I have followed the steps, but I got this error:

FatalErrorException in FlyersController.php line 87:
Call to a member function move() on null

The files:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Flyer;
use App\Photo;

use App\Http\Requests\FlyerRequest;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class FlyersController extends Controller
{

    public function __construct() {

      $this->middleware('auth', ['except' => ['show']]);

    }

    /**
    * Display a listing of the resource.
    *
    * @return Response
    */

    public function index() {

    }

    public function create() {
      return view('flyers.create');
    }

    /**
    * Store a newly created resource in storage.
    *
    * @param FlyerRequest $request
    * @return Response
    */

    public function store(FlyerRequest $request) {

      Flyer::create($request->all());

      flash()->success('Success!', 'Your flyer has been created.');

      return redirect()->back(); // temporary
    }

    /**
    * @param int $id
    * @return Response
    **/

    public function show($zip, $street) {

      $flyer = Flyer::locatedAt($zip, $street);

      return view('flyers.show', compact('flyer'));

    }

    /**
    * Apply a photo to the referenced flyer.
    *
    * @param string $zip
    * @param string $street
    * @param Request $request
    */

    public function addPhoto($zip, $street, Request $request) {

      $this->validate($request, [
        'photo' => 'required|mimes:jpg,jpeg,png,bmp'
      ]);

      $photo = $this->makePhoto($request->file('photo'));

      $flyer = Flyer::locatedAt($zip, $street)->addPhoto($photo);

    }

    protected function makePhoto(UploadedFile $file) {

      return Photo::named($file->getClientOriginalName())->move($file);

    }

}
<?php

namespace App;

use Symfony\Component\HttpFoundation\File\UploadedFile;

use Intervention\Image\ImageServiceProvider;
use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{

    protected $table = 'flyer_photos';

    protected $fillable = ['path', 'name', 'thumbnail_path'];

    protected $baseDir = 'flyers/photos';

    public function flyer() {
      return $this->belongsTo('App\Flyer');
    }

    /**
     * Build a new photo instance from a file upload.
     *
     * @param string $name
     * @param self
     */

    public static function named($name) {

      return (new static)->saveAs($name);
      
    }

    protected function saveAs($name) {

      $this->name = sprintf("%s-%s", time(), $name);
      $this->path = sprintf("%s/%s", $this->baseDir, $this->name);
      $this->thumbnail_path = sprintf("%s/tn-%s", $this->baseDir, $this->name);

    }

    public function move(UploadedFile $file) {

      $file->move($this->baseDir, $this->name);

      $this->makeThumbnail();

      return $this;

    }

    protected function makeThumbnail() {
      Image::make($this->path)
        ->fit(200)
        ->save($this->thumbnail_path);
    }

}

What could be wrong?

0 likes
0 replies

Please or to participate in this conversation.