mardi 6 juin 2017

Attempt to assign property of non-object in Laravel 5

I have the following Laravel code which receive data from an API and store them on a database.

$sample = Sample::findOrFail($id);
$new_sample = $request->except('filename');

if ( $request->file('filename') ) {
     if ( $sample->filename ) {
           Storage::disk('documents')->delete($sample->filename);
     }
     $file = $request->file('filename');
     $extension = $file->getClientOriginalExtension();
     $new_sample->filename = $file->getFilename().'.'.$extension;
     Storage::disk('documents')->put($file->getFilename().'.'.$extension,  File::get($file));
}

if (!$sample->update($new_sample)) {
     throw new HttpException(500);
}

I have the following error Attempt to assign property of non-object and I found that by commenting out the line where I assign the filename, the error doesn't appear again.

$new_sample->filename = $file->getFilename().'.'.$extension;

So, I guess the problem is here. I tried also with

$new_sample['filename'] = $file->getFilename().'.'.$extension;

but then it doesn't store the filename.

Any idea?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire