J'ai besoin d'aide pour supprimer une photo de mon dossier dans Laravel 5.2. Lorsque je supprime une photo, elle supprime la db mais pas le dossier.Supprimer Photo du dossier Laravel 5.2
Voici mon itinéraire
Route::group(['middleware' => ['web']], function() {
Route::resource('flyer', 'FlyersController');
Route::delete('photos/{id}', [
'uses' => '\App\Http\Controllers\[email protected]',
'as' => 'flyer.destroy'
]);
});
Voici mon Photo.php Modèle: (Supprimer la photo est en bas)
<?php
namespace App;
use Image;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Photo extends Model {
/**
* @var string
* The associated table.
*/
protected $table = "flyer_photos";
/**
* @var array
* Fillable fields for a photo.
*/
protected $fillable = ['name', 'path', 'thumbnail_path'];
/**
* @var
* The UploadedFile instance.
*/
protected $file;
/**
* @var
* The file name instance.
*/
protected $name;
/**
* A photo belongs to a flyer
*/
public function flyer() {
return $this->belongsTo('App\Flyer');
}
/**
* Make a new instance from an uploaded file.
*/
public static function fromFile(UploadedFile $file) {
// Make a new instance.
$photo = new static;
// Assign the Uploaded file to the $file object.
$photo->file = $file;
// Set $photo to the fill properties, which are
// the name, path, and thumbnail path of a photo.
$photo->fill([
'name' => $photo->setFileName(),
'path' => $photo->filePath(),
'thumbnail_path' => $photo->thumbnailPath()
]);
// Then return the photo.
return $photo;
}
/**
* Get the photos base directory.
*/
public function baseDir() {
return 'FlyerPhotos/photos';
}
/**
* This function gets the name and extension of a photo.
*/
public function setFileName() {
// Set $t = to time()
$t = time();
// This will reduce the likelihood of a double call because it will
// only be a problem if the calls spanned a minute (vs a second),
$t -= $t % 60;
// hash the name of the file with the $t function.
$hash = sha1(
$t . $this->file->getClientOriginalName()
);
// Get the extension of the photo.
$extension = $this->file->getClientOriginalExtension();
// Then set name = merge those together.
return $this->name = "{$hash}.{$extension}";
}
/**
* Get the full file path of the photo, with the name.
*/
public function filePath() {
return $this->baseDir() . '/' . $this->name;
// Ex: 'FlyerPhotos/photos/foo.jpg'
}
/**
* Get the full file thumbnail path of the photo, with the name.
*/
public function thumbnailPath() {
return $this->baseDir() . '/tn-' . $this->name;
// Ex: 'FlyerPhotos/photos/tn-foo.jpg'
}
/**
* Upload the file to the proper directory.
*/
public function upload() {
// Move the file instance to the base directory with the file name.
$this->file->move($this->baseDir(), $this->name);
// Make the thumbnail.
$this->makeThumbnail();
return $this;
}
/**
* Function to make the actual thumbnail.
* -- make and save reference the Image intervention library, not Eloquent. --
*/
protected function makeThumbnail() {
Image::make($this->filePath())->fit(200)->save($this->thumbnailPath());
}
/**
* Delete the photo path and thumbnail path in DB.
* Access the delete function in [email protected] method
*/
public function delete() {
\File::delete([
$this->filePath(),
$this->thumbnailPath()
]);
parent::delete();
}
}
Voici mon contrôleur: (abrégé)
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use App\Flyer;
use App\Photo;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Requests\FlyerRequest;
use App\Http\Controllers\Controller;
use App\Http\Requests\AddPhotoRequest;
use App\Http\Requests\UserEditRequest;
class FlyersController extends Controller {
/**
* Add photos to flyer.
*/
public function addPhoto($zip, $street, AddPhotoRequest $request) {
// Set $photo to the fromFile() function,
// and get the $requested file which is set to 'photo',
// and upload it using the upload function().
// -- 'photo' comes from the <script> tags in show.blade.php.
// Create a new photo instance from a file upload.
$photo = Photo::fromFile($request->file('photo'))->upload();
// Set Flyer::loacatedAt() in (Flyer Model)
// = to the zip and street, and add the photo.
// -- Find the flyer and add the photo.
Flyer::locatedAt($zip, $street)->addPhoto($photo);
}
/**
* Delete a photo.
* -- Access delete() in Photo.php Model --
*/
public function destroyPhoto($id) {
Photo::findOrFail($id)->delete();
return redirect()->back();
}
}
Et voici mon avis:
<div class="img-wrap">
<form method="post" action="{{ route('flyer.destroy', ['id' => $photo->id]) }}" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="DELETE">
@if ($user && $user->owns($flyer))
<button type="submit" class="close">×</button>
@endif
<a href="/project-flyer/{{ $photo->path }}" data-lity>
<img src="/project-flyer/{{ $photo->thumbnail_path }}" alt="" data-id="{{ $photo->id }}">
</a>
</form>
</div>
Ceci est mon chemin pour stocker mes photos:
(image et miniatures stockées dans le même dossier, photo qui commence par un tn - est la vignette)
J'ai essayé les deux sens et il me donne juste url pas trouvé – David
peut-être quelque chose de mal avec la fonction de suppression() dans Photo.php? – David
non, non trouvé signifie que votre itinéraire est erroné - vous tapez la mauvaise URL (pas celle de routes.php). vous verriez une erreur différente si votre application atteignait le contrôleur. cochez l'onglet "Réseau" dans Chrome et voyez où se trouve exactement votre site lorsque vous cliquez sur "Supprimer". comparez-le à l'entrée 'php artisan route: list' - vous verrez où se trouve l'erreur! –