Azam Rezayi преди 4 години
родител
ревизия
00f8704207

+ 4 - 3
app/Models/Upload.php

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

+ 3 - 3
database/migrations/2020_07_05_100449_create_uploadables_table.php

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

+ 46 - 23
packages/product/src/Http/Controllers/ProductController.php

@@ -65,8 +65,6 @@ class ProductController extends Controller
             'price' => preg_replace('/[^0-9]+/', '', $request->price),
             'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
         ]);
-
-
         $data = [
             'title' => $request->title,
             'slug' => $request->slug,
@@ -78,37 +76,22 @@ class ProductController extends Controller
             'discription' => $request->discription,
             'creator_id' => auth()->user()->id
         ];
-
         $product = Product::create($data);
+        $product->categories()->sync($request->categories);
 
-        $file = $request->file('photo');
-//       $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate('m');
-
-        $fileMime = $file->getClientMimeType();
-        $fileExtension = $file->getClientOriginalExtension();
-        $fileName =  'product_file'. time() . '_'  . $file->getClientOriginalName();
-//    /    $filePath = $file->storeAs('photos' . .$afterDiskRoot ,$fileName,'product');
 
-      $filePath = $file->storeAs('photos',  $fileName , 'product');
+        if ($request->has('photo')) {
+            $file = $request->file('photo');
+            $diskName = 'product';
+            $this->uploader($file, $diskName, $product);
 
-        if (file_exists($filePath)) {
 
-           $fileName = 'product_file'. time() . '_' . $fileName;
 
         }
-        $dataUpload = [
-            'name' => $fileName,
-            'extension' => $fileExtension,
-            'path' => $filePath,
-            'mime_type' => $fileMime,
-            'uploadable_type' => 'Packages\Product\Models\Product',
-            'uploadable_id' => $product->id,
 
-        ];
+        $product->uploads()->sync($request->uploads);
 
 
-        $product->categories()->sync($request->categories);
-        $product->uploads()->create($dataUpload);
 
 
         $msg = 'ذخیره محصول با موفقیت انجام شد ';
@@ -189,6 +172,46 @@ class ProductController extends Controller
         }
     }
 
+    public function uploader($file, $diskName, $product)
+    {
+        $fileExtension = $file->getClientOriginalExtension();
+        $fileMimeType = $file->getMimeType();
+
+        $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
+
+        $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
+
+        $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
+
+        if (File_exists($filePath)) {
+            $fileName = time() . '_' . $fileName;
+        }
+
+        $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
+
+        $uploadData = [
+            'name' => $fileName,
+            'path' => $upload,
+            'mime_type' => $fileMimeType,
+            'extension' => $fileExtension,
+
+        ];
+
+        $uploaded = $product->uploads()->create($uploadData);
+
+    }
+    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);
+    }
 
 
 }

+ 2 - 2
packages/product/src/Models/Product.php

@@ -12,7 +12,7 @@ class Product extends Model
 {
     use SoftDeletes;
 
-    protected $fillable = ['title', 'discription', 'price', 'creator_id', 'type', 'status', 'sale_price', 'sku', 'slug'];
+    protected $fillable = ['title', 'discription', 'price', 'creator_id', 'type', 'status', 'sale_price', 'sku', 'slug', 'photo'];
     //protected $guarded = [];
 
     public function user()
@@ -27,7 +27,7 @@ class Product extends Model
     }
     public function uploads()
     {
-        return $this->morphToMany('App\Models\Upload', 'uploadables');
+        return $this->morphToMany(Upload::class, 'uploadable');
     }
     public function getTypeAttribute($value)
     {