Level 42
@Ahava Flyer::locatedAt($zip, $street) does not find the record and returns null. Check the contents of $zip and $street and that your database table actually contains a record with those values (after transforming $street).
Hi,
I've just followed the video "Bulk File Uploads" but I'm getting the following error when uploading a file.
FatalErrorException in FlyersController.php line 44:
Call to a member function addPhoto() on null
in FlyersController.php line 44
My FlyersController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\FlyerRequest;
use App\Http\Controllers\Controller;
use App\Flyer;
use App\Photo;
class FlyersController extends Controller
{
public function create()
{
return view('flyers.create');
}
public function store(FlyerRequest $request)
{
Flyer::create($request->all());
flash()->success('Success!', 'Your flyer has been created.');
return redirect()->back();
}
public function show($zip, $street)
{
$flyer = Flyer::locatedAt($zip, $street);
return view('flyers.show', compact('flyer'));
}
public function addPhoto($zip, $street, Request $request)
{
$this->validate($request, [
'photo' => 'required|mimes:jpg,jpeg,png,bmp,gif'
]);
$photo = Photo::fromForm($request->file('photo'));
Flyer::locatedAt($zip, $street)->addPhoto($photo);
}
}
My Flyer.php Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flyer extends Model
{
protected $fillable = [
'street',
'city',
'state',
'country',
'zip',
'price',
'description'
];
public static function locatedAt($zip, $street)
{
$street = str_replace('-', ' ', $street);
return static::where(compact('zip', 'street'))->first();
}
public function getPriceAttribute($price)
{
return '$' . number_format($price);
}
public function addPhoto(Photo $photo)
{
return $this->photos()->save($photo);
}
public function photos()
{
return $this->hasMany('App\Photo');
}
}
@Ahava Flyer::locatedAt($zip, $street) does not find the record and returns null. Check the contents of $zip and $street and that your database table actually contains a record with those values (after transforming $street).
Please or to participate in this conversation.