Răsfoiți Sursa

make table file and make table uploadable

azam 4 ani în urmă
părinte
comite
85d516a3b3

+ 16 - 0
app/Models/Upload.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Packages\Product\Models\Product;
+
+
+class Upload extends Model
+{
+    protected $fillable = ['name', 'path', 'mime_type', 'creator_id', 'uploadable'];
+    public function products()
+    {
+        return $this->morphTo(Product::class, 'uploadable');
+    }
+}

+ 1 - 0
composer.json

@@ -16,6 +16,7 @@
         "laravel/framework": "^7.0",
         "laravel/tinker": "^2.0",
         "laravel/ui": "^2.0",
+        "league/flysystem-cached-adapter": "~1.0",
         "santigarcor/laratrust": "^6.0",
         "uxweb/sweet-alert": "^2.0",
         "va/cutlet-recaptcha": "^1.1"

+ 94 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "dd030fecb961252239cab6c43dd1a564",
+    "content-hash": "978622f37b7dcfc6d245cbfd8a18d3a9",
     "packages": [
         {
             "name": "asm89/stack-cors",
@@ -1207,6 +1207,53 @@
             "time": "2020-04-16T13:21:26+00:00"
         },
         {
+            "name": "league/flysystem-cached-adapter",
+            "version": "1.0.9",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/thephpleague/flysystem-cached-adapter.git",
+                "reference": "08ef74e9be88100807a3b92cc9048a312bf01d6f"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/thephpleague/flysystem-cached-adapter/zipball/08ef74e9be88100807a3b92cc9048a312bf01d6f",
+                "reference": "08ef74e9be88100807a3b92cc9048a312bf01d6f",
+                "shasum": ""
+            },
+            "require": {
+                "league/flysystem": "~1.0",
+                "psr/cache": "^1.0.0"
+            },
+            "require-dev": {
+                "mockery/mockery": "~0.9",
+                "phpspec/phpspec": "^3.4",
+                "phpunit/phpunit": "^5.7",
+                "predis/predis": "~1.0",
+                "tedivm/stash": "~0.12"
+            },
+            "suggest": {
+                "ext-phpredis": "Pure C implemented extension for PHP"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "League\\Flysystem\\Cached\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "frankdejonge",
+                    "email": "info@frenky.net"
+                }
+            ],
+            "description": "An adapter decorator to enable meta-data caching.",
+            "time": "2018-07-09T20:51:04+00:00"
+        },
+        {
             "name": "monolog/monolog",
             "version": "2.0.2",
             "source": {
@@ -1528,6 +1575,52 @@
             "time": "2020-03-21T18:07:53+00:00"
         },
         {
+            "name": "psr/cache",
+            "version": "1.0.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/cache.git",
+                "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8",
+                "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\Cache\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common interface for caching libraries",
+            "keywords": [
+                "cache",
+                "psr",
+                "psr-6"
+            ],
+            "time": "2016-08-06T20:24:11+00:00"
+        },
+        {
             "name": "psr/container",
             "version": "1.0.0",
             "source": {

+ 1 - 1
config/app.php

@@ -174,7 +174,7 @@ return [
         // App\Providers\BroadcastServiceProvider::class,
         App\Providers\EventServiceProvider::class,
         App\Providers\RouteServiceProvider::class,
-       
+
 
     ],
 

+ 6 - 0
config/filesystems.php

@@ -54,6 +54,12 @@ return [
             'url' => env('APP_URL').'/storage',
             'visibility' => 'public',
         ],
+        'product' => [
+            'driver' => 'local',
+            'root' => storage_path('app/public/product'),
+            'url' => env('APP_URL').'/storage/product',
+            'visibility' => 'public',
+        ],
 
         's3' => [
             'driver' => 's3',

+ 35 - 0
database/migrations/2020_07_04_064817_create_upload_files_table.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateUploadFilesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('upload_files', function (Blueprint $table) {
+            $table->id();
+            $table->string('name');
+            $table->string('path');
+            $table->string('mime_type');
+            $table->morphs('uploadable');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('upload_files');
+    }
+}

+ 32 - 0
database/migrations/2020_07_04_080635_create_uploadable_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateUploadableTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('uploadable', function (Blueprint $table) {
+            $table->integer('upload_id');
+            $table->integer('uploadable_id');
+            $table->string('uploadable_type');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('uploadable');
+    }
+}

+ 5 - 1
packages/product/src/Models/Product.php

@@ -2,6 +2,7 @@
 
 namespace Packages\Product\Models;
 use App\Models\Category;
+use App\Models\Upload;
 use Illuminate\Database\Eloquent\Model;
 use App\User;
 use Illuminate\Database\Eloquent\SoftDeletes;
@@ -24,7 +25,10 @@ class Product extends Model
     {
         return $this->morphToMany(Category::class, 'categorizable');
     }
-
+    public function upload()
+    {
+        return $this->morphToMany(Upload::class, 'uploadable');
+    }
     public function getTypeAttribute($value)
     {
 

+ 15 - 5
packages/product/src/views/product/create.blade.php

@@ -22,6 +22,9 @@
             select2 {
                 background-color: hsl(315, 22%, 86%);
             }
+            .photo{
+                border: 0px ;background-color: #fff;
+            }
         </style>
     @endslot
     @slot('subject')
@@ -42,7 +45,7 @@
                             @component('product::components.message')
                             @endcomponent
                             <div class="card-body mx-lg-5 px-lg-3">
-                                <form method="POST" action="{{ route('products.store') }}">
+                                <form method="POST" action="{{ route('products.store') }}" enctype="multipart/form-data">
                                     @csrf
 
                                     <div class="form-group">
@@ -171,14 +174,10 @@
                                                 name="categories[]" multiple>
 
                                             <option value="">انتخاب کنید</option>
-
-
                                             @foreach($categories as $cat)
-
                                                 <option value="{{$cat->id }}">{{$cat->title}}</option>
                                             @endforeach
                                         </select>
-
                                         @error('categories')
                                         <span class="invalid-feedback" role="alert">
                                             <strong>{{ $message }}</strong>
@@ -186,6 +185,17 @@
                                         @enderror
 
                                     </div>
+                                    <div class="form-group">
+                                        <label for="title">{{ __('product.photo') }}</label><i
+                                            class="required">&nbsp; *</i>
+                                        <input type="file" name="photo" id="photo"
+
+                                               class="form-control photo @error('photo') is-invalid  @enderror"
+                                               value="{{old('photo')}}" autocomplete="photo" autofocus >
+                                        @error('photo')
+                                        <div class="alert alert-danger">{{$message}}</div>
+                                        @enderror
+                                    </div>
                                     <div class="form-group mb-0">
                                         <div class="">
                                             <button type="submit" class="btn btn-primary">

+ 2 - 2
packages/product/src/views/product/edit.blade.php

@@ -156,7 +156,7 @@
                                         @enderror
                                     </div>
                                     <div class="form-group">
-                                        <label for="categories[]">{{ __('product.categories.*') }}</label><i
+                                        <label for="categories[]">{{ __('product.categories') }}</label><i
                                             class="required">&nbsp; *</i>
                                         <select id="categories" type="text"
                                                 class="form-control select2 @error('categories[]') is-invalid @enderror"
@@ -164,7 +164,7 @@
                                                 autocomplete="categories[]" multiple>
                                             @foreach($categories as $cat)
                                                 <option value="{{$cat->id}}" <?php
-                                                    if (in_array($cat->id, $product->categories->pluck('id')->toArray())) echo 'selected'
+                                                    if (in_array($cat->id,$product->categories->pluck('id')->toArray())) echo 'selected'
                                                     ?>>{{$cat->title}}</option>
                                             @endforeach
                                         </select>

+ 1 - 0
resources/lang/fa/product.php

@@ -9,5 +9,6 @@ return [
   'sku'   => 'sku ',
   'slug'   => 'slug',
   'title'   => 'عنوان',
+  'photo'   => 'عکس محصول',
 ];