Răsfoiți Sursa

Murphy's review between uploading with products

Azam Rezayi 4 ani în urmă
părinte
comite
be8ca06b2a

+ 3 - 4
app/Models/Upload.php

@@ -8,10 +8,9 @@ use Packages\Product\Models\Product;
 
 class Upload extends Model
 {
-    protected $guarded = [];
-//   protected $fillable = ['name', 'path', 'mime_type', 'extension', 'uploadable_type', 'uploadable_id'];
-    public function uploadable()
+    protected $fillable = ['name', 'path', 'mime_type', 'extension', 'uploadable_type', 'uploadable_id'];
+    public function products()
     {
-        return $this->morphTo();
+        return $this->morphTo('Packages\Product\Models\Product', 'uploadable');
     }
 }

+ 4 - 5
database/migrations/2020_07_05_100449_create_uploadables_table.php → database/migrations/2020_07_06_095717_create_uploadables_table.php

@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
-class CreateUploadableTable extends Migration
+class CreateUploadablesTable extends Migration
 {
     /**
      * Run the migrations.
@@ -13,11 +13,10 @@ class CreateUploadableTable extends Migration
      */
     public function up()
     {
-        Schema::create('uploadable', function (Blueprint $table) {
-
+        Schema::create('uploadables', function (Blueprint $table) {
             $table->integer('upload_id');
-            $table->integer('uploadables_id');
-            $table->string('uploadables_type');
+            $table->integer('uploadable_id');
+            $table->string('uploadable_type');
             $table->timestamps();
         });
     }

+ 19 - 44
packages/product/src/Http/Controllers/ProductController.php

@@ -85,31 +85,25 @@ class ProductController extends Controller
             $diskName = 'product';
             $this->uploader($file, $diskName, $product);
 
-
-
         }
 
-        $product->uploads()->sync($request->uploads);
-
-
-
-
         $msg = 'ذخیره محصول با موفقیت انجام شد ';
         return redirect(route('products.edit', $product->id))->with('success', $msg);
     }
 
     // ******************************************************edit page product*********************************************
 
-    public function edit(Product $product)
+    public function edit( Product $product)
     {
 
         $categories = Category::orderBy('id', 'DESC')->get();
-        return view('product::product.edit', compact('product', 'categories'));
+        $upload = upload::orderBy('id', 'DESC')->get();
+        return view('product::product.edit', compact('product', 'categories', 'upload'));
     }
 
 // *******************************************update page product*****************************************
 
-    public function update(ProductRequest $request, Product $product, Upload $upload)
+    public function update(ProductRequest $request, Product $product)
     {
 
         $request->merge([
@@ -132,30 +126,9 @@ class ProductController extends Controller
 
         $product->update($data);
 
+        $product->categories()->sync($request->categories);
 
-        $file = $request->file('photo');
-        $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate('m');
-        $diskName = 'product';
-        $fileMime = $file->getClientMimeType();
-        $fileExtension = $file->getClientOriginalExtension();
-        $fileName = 'product-file-' . '_' . $file->getClientOriginalName();
-        $filePath = $file->storeAs('photos', $fileName, 'product');
-        if (file_exists($filePath)) {
 
-            $fileName = time() . '_' . $fileName;
-
-        }
-        $dataUpload = [
-            'name' => $fileName,
-            'extension' => $fileExtension,
-            'path' => $filePath,
-            'mime_type' => $fileMime,
-            'uploadable_type' => 'Packages\Product\Models\Product',
-            'uploadable_id' => $product->id,
-
-        ];
-        $product->categories()->sync($request->categories);
-        $product->uploads()->update($dataUpload);
         $msg = 'ویرایش محصول با موفقیت انجام شد ';
         dd($request->all());
         return redirect(route('products.index'),compact('upload'))->with('success', $msg);
@@ -189,6 +162,7 @@ class ProductController extends Controller
 
         $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
 
+
         $uploadData = [
             'name' => $fileName,
             'path' => $upload,
@@ -198,20 +172,21 @@ class ProductController extends Controller
         ];
 
         $uploaded = $product->uploads()->create($uploadData);
+        dd($uploaded);
 
     }
-    public function serveFile(Request $request, $id)
-    {
-        $attachment = Upload::where('uploadable_type', 'Packages\Product\Models\Product')
-            ->where('id', $id)
-            ->firstOrFail();
-
-        if (!\Storage::disk('product')->exists($attachment->path))
-            abort(404);
-        $file_path = \Storage::disk('product')->getDriver()->getAdapter()->getPathPrefix() . $attachment->path;
-
-        return response()->file($file_path);
-    }
+//    public function serveFile(Request $request, $id)
+//    {
+//        $attachment = Upload::where('uploadable_type', 'Packages\Product\Models\Product')
+//            ->where('id', $id)
+//            ->firstOrFail();
+//
+//        if (!\Storage::disk('product')->exists($attachment->path))
+//            abort(404);
+//        $file_path = \Storage::disk('product')->getDriver()->getAdapter()->getPathPrefix() . $attachment->path;
+//
+//        return response()->file($file_path);
+//    }
 
 
 }

+ 1 - 1
packages/product/src/Http/Requests/ProductRequest.php

@@ -31,7 +31,7 @@ class ProductRequest extends FormRequest
             'status' => ['required'],
             'discription' => ['required'],
             'categories' => ['required'],
-            'photo' => ['required','file','image','mimes:jpeg,jpg,npg,gif,webp,jfif','max:2048'],
+//            'photo' => ['required','file','image','max:2048'],
 
         ];
 

+ 1 - 0
packages/product/src/routes/web.php

@@ -11,6 +11,7 @@ Route::group([
     Route::resource('products','ProductController');
 ;
     Route::post('products/{product}', 'ProductController@index')->name('products.search');
+    Route::post('products/{path}', 'ProductController@serveFile')->name('products.serveFile');
 
 
 

+ 5 - 4
packages/product/src/views/product/edit.blade.php

@@ -180,19 +180,20 @@
 
                                         <label for="title">{{ __('product.photo') }}</label><i
                                             class="required">&nbsp; *</i>
-                                        @foreach($product->uploads as $upload_photo)
 
-                                          <span>{{basename($upload_photo->path)}}</span>
+{{--                                        @foreach($product->uploads as $upload_photo)--}}
+{{--                                            <a href="{{route('products.serveFile',[$id => $attachment->id])}}"></a>--}}
+{{--                                          <span>{{basename($upload_photo->path)}}</span>--}}
                                         <input type="file" name="photo" id="photo"
 
                                                class="form-control photo @error('photo') is-invalid  @enderror"
-                                               value="{{basename($upload_photo->name)}}" autocomplete="photo" autofocus >
+                                               value="" autocomplete="photo" autofocus >
                                         @error('photo')
                                         <span class="invalid-feedback" role="alert">
                                             <strong>{{ $message }}</strong>
                                         </span>
                                         @enderror
-                                        @endforeach
+{{--                                        @endforeach--}}
                                     </div>
                                     <div class="form-group mb-0">
                                         <div class="">