php - Laravel Dropzone.js Image not saving -
i have managed dropzone working , upload pictures laravel project. problem having is not saving when page reloads image gone. can me identify going wrong? in advance pointers appreciated.
here html:
<form method="post" action="/admin/products/{{ $products->id }}/photo" class="dropzone" id="addproductimages" enctype="multipart/form-data"> {{ csrf_field() }} </form>
here controller:
namespace app\http\controllers; use illuminate\http\request; use app\product; use app\productphoto; use app\addphototoproduct; use app\http\requests\productphotorequest; use illuminate\support\facades\input; class productphotoscontroller extends controller { /** * @param $id * @param productphotorequest $request */ // public function store($id,request $request) { $product = product::locatedat($id); $files = $request->file('file'); foreach ($files $file) { (new addphototoproduct($product,$file))->save(); } } }
request file:
namespace app\http\requests; use app\product; use illuminate\foundation\http\formrequest; class productphotorequest extends formrequest { /** * determine if user authorized make request. * * @return bool */ public function authorize() { return product::where([ 'id' => $this->id, ])->exists(); }
model:
namespace app; use illuminate\database\eloquent\model; use symfony\component\httpfoundation\file\uploadedfile; class productphoto extends model { /** * @var string * associated table. */ protected $table = "product_images"; /** * @var array */ protected $fillable = ['name', 'path', 'thumbnail_path', 'featured']; /** * product photos belong product. * * @return \illuminate\database\eloquent\relations\belongsto */ public function product() { return $this->belongsto('app\product'); } /** * product photos base directory. */ public function basedir() { return 'src/public/productphotos/photos'; } /** * set name attribute of photo * * * @param $name */ public function setnameattribute($name) { $this->attributes['name'] = $name; // set path of photo $this->path = $this->basedir() . '/' . $name; // set thumbnail path of photo $this->thumbnail_path = $this->basedir() . '/th-' . $name; }
and:
namespace app; use symfony\component\httpfoundation\file\uploadedfile; class addphototoproduct { /** * @var product */ protected $product; /** * uploadedfile instance. * * @var uploadedfile */ protected $file; /** * create new addphototoproduct form object. * * @param product $product * @param uploadedfile $file * @param thumbnail|null $thumbnail */ public function __construct(product $product, uploadedfile $file, thumbnail $thumbnail = null) { $this->product = $product; $this->file = $file; $this->thumbnail = $thumbnail ?: new thumbnail; } /** * process form. */ public function save() { // attach photo product. $photo = $this->product->addphoto($this->makephoto()); // move file base directory file name. $this->file->move($photo->basedir(), $photo->name); // generate photo thumbnail. $this->thumbnail->make($photo->path, $photo->thumbnail_path); } /** * make new photo instance. * * @return productphoto */ protected function makephoto() { return new productphoto(['name' => $this->makefilename()]); } /** * make filename, based on uploaded file. * * @return string */ protected function makefilename() { // file name original name // , encrypt sha1 $name = sha1 ( time() . $this->file->getclientoriginalname() ); // extension of photo. $extension = $this->file->getclientoriginalextension(); // set name = merge together. return "{$name}.{$extension}"; } }
Comments
Post a Comment