浏览代码

make input price

azam 4 年之前
父节点
当前提交
6773de8a54

+ 6 - 3
app/Http/Controllers/packages/Categories/CategoryController.php → app/Http/Controllers/Panel/Categories/CategoryController.php

@@ -4,7 +4,7 @@ namespace App\Http\Controllers\Panel\Categories;
 use App\Models\Category;
 use App\Http\Controllers\Controller;
 use Illuminate\Http\Request;
-use Packages\Product\Models\Product;
+
 
 class CategoryController extends Controller
 {
@@ -122,8 +122,11 @@ class CategoryController extends Controller
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
-    public function destroy($id)
+    public function destroy(Category $category)
     {
-        //
+        $category->delete();
+        {
+            return back();
+        }
     }
 }

+ 26 - 0
app/Models/Category.php

@@ -2,6 +2,7 @@
 
 namespace App\Models;
 use App\User;
+use Packages\Product\Models\Product;;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\SoftDeletes;
 class Category extends Model
@@ -16,4 +17,29 @@ class Category extends Model
         return $this->belongsTo(User::class, 'creator_id', 'id');
     }
 
+    public function category()
+    {
+        return $this->morphedByMany('Packages\Product\Models', 'categorizable');
+    }
+    public function getTypeAttribute($value)
+    {
+
+        switch ($value) {
+            case 0:
+                $type= 'ساده';
+                break;
+            case 1:
+                $type= 'متغیر';
+                break;
+            case 2:
+                $type= 'باندل';
+                break;
+
+            default:
+                $type='';
+
+        }
+        return $type;
+    }
+
 }

+ 0 - 31
database/migrations/2020_06_21_112600_create_product_category.php

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

+ 8 - 4
database/migrations/2020_06_23_051330_create_object_category_table.php → database/migrations/2020_06_23_111929_craete_categorizables_tabla.php

@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
-class CreateObjectCategoryTable extends Migration
+class CraeteCategorizablesTabla extends Migration
 {
     /**
      * Run the migrations.
@@ -13,10 +13,14 @@ class CreateObjectCategoryTable extends Migration
      */
     public function up()
     {
-        Schema::create('object_category', function (Blueprint $table) {
+        Schema::create('categorizables', function (Blueprint $table) {
             $table->id();
-            $table->integer('object_id');
             $table->integer('category_id');
+            $table->integer('categorizable_id');
+            $table->string('categorizable_type');
+
+
+
         });
     }
 
@@ -27,6 +31,6 @@ class CreateObjectCategoryTable extends Migration
      */
     public function down()
     {
-        Schema::dropIfExists('object_category');
+        //
     }
 }

+ 18 - 2
packages/product/src/Http/Controllers/ProductController.php

@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
 use Packages\Product\Models\User;
 use Packages\Product\Models\Product;
 use Illuminate\Http\Request;
+use App\Models\Category;
 
 class ProductController extends Controller
 {
@@ -29,8 +30,9 @@ class ProductController extends Controller
     public function create()
     {
         $products = Product::orderBy('id', 'DESC')->paginate(20);
+        $categories = Category::orderBy('id', 'DESC')->get();
 
-        return view('product::product.create', compact('products'));
+        return view('product::product.create', compact('products', 'categories'));
 
     }
 
@@ -38,6 +40,7 @@ class ProductController extends Controller
 
     public function store(Request $request)
     {
+
         $validatedData= $request->validate([
             'title' => 'required|max:100',
             'price' => 'required',
@@ -54,11 +57,20 @@ class ProductController extends Controller
             'type.required' => 'فیلد نوع محصول الزامی می باشد.',
 
         ]);
+        $request->merge([
+            'price' => preg_replace( '/[^0-9]+/', '', $request->price ),
+            'sale_price' => preg_replace( '/[^0-9]+/', '', $request->sale_price ),
+            ]);
+
       $data = array_merge($request->all(),['creator_id' => auth()->user()->id]);
-//        $data = User::all()->pluck('name','id');
+
         $product = Product::create($data);
 
 
+
+        $product->categories()->attach($request->categories);
+
+
         $msg = 'ذخیره محصول با موفقیت انجام شد ';
         return redirect(route('products.edit', $product->id))->with('success', $msg);
     }
@@ -91,6 +103,10 @@ class ProductController extends Controller
             'type.required' => 'فیلد نوع محصول الزامی می باشد.',
 
         ]);
+        $request->merge([
+            'price' => preg_replace( '/[^0-9]+/', '', $request->price ),
+            'sale_price' => preg_replace( '/[^0-9]+/', '', $request->sale_price ),
+        ]);
         $data = array_merge($request->all(),['creator_id'=>auth()->user()->id]);
             $product->update($data);
 

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

@@ -5,7 +5,7 @@ namespace Packages\Product\Models;
 use Illuminate\Database\Eloquent\Model;
 use App\User;
 use Illuminate\Database\Eloquent\SoftDeletes;
-
+use App\Models\Category;
 class Product extends Model
 {
     use SoftDeletes;
@@ -17,7 +17,10 @@ class Product extends Model
         return $this->belongsTo(User::class, 'creator_id', 'id');
     }
 
-
+    public function categories()
+    {
+        return $this->morphToMany('App\Models\Category', 'categorizable');
+    }
 
 
     public function getTypeAttribute($value)

+ 14 - 4
packages/product/src/views/index.blade.php

@@ -44,12 +44,13 @@
                                     <th>slug</th>
                                     <th> sku</th>
                                     <th>قیمت</th>
-                                    <th>قیمت فروش</th>
+                                    <th> قیمت فروش ویژه</th>
                                     <th> نویسنده</th>
                                     <th>وضعیت</th>
                                     <th>نوع</th>
-                                    <th>توضیحات</th>
 
+                                    <th>توضیحات</th>
+                                    <th>دسته بندی</th>
                                     <th> مدیریت</th>
                                 </tr>
                             @endslot
@@ -85,9 +86,18 @@
                                         </td>
                                         <td>
                                             {{$product->type}}
-                                            {{--                                            {!!$type!!}--}}
+
+                                        </td>
+
+                                        <td>
+                                            <?php  echo mb_substr(strip_tags($product->discription), 0, 15, 'UTF8') . '...'?>
+                                        </td>
+                                        <td>
+
+                                            @foreach($product->categories->pluck('name') as $category)
+                                                {{$category}}
+                                            @endforeach
                                         </td>
-                                        <td><?php  echo mb_substr(strip_tags($product->discription), 0, 15, 'UTF8') . '...'?></td>
                                         <td class="d-flex">
                                             <a href="{{route('products.edit', $product->id)}}"
                                                class="btn btn-sm btn-primary mr-2">ویرایش</a>

+ 21 - 7
packages/product/src/views/product/create.blade.php

@@ -75,9 +75,9 @@
                                     </div>
                                     <div class="form-group">
                                         <label for="title">{{ __('قیمت ') }}</label><i class="required">&nbsp; *</i>
-                                        <input id="price" type="text"
-                                               class="form-control @error('price') is-invalid @enderror" name="price"
-                                               value="{{ old('price') }}"  autocomplete="price" autofocus>
+                                        <input id="input.price" type="text"
+                                               class="form-control price @error('price') is-invalid @enderror" name="price"
+                                               value="{{ old('price') }}"  autocomplete="price" autofocus >
 
                                         @error('price')
                                         <span class="invalid-feedback" role="alert">
@@ -90,7 +90,7 @@
                                     <div class="form-group">
                                         <label for="sale_price">{{ __('  قیمت فروش ویژه') }}</label>
                                         <input id="sale_price" type="text"
-                                               class="form-control @error('sale_price') is-invalid @enderror"
+                                               class="form-control price @error('sale_price') is-invalid @enderror"
                                                name="sale_price"
                                                value="{{ old('sale_price') }}" autocomplete="sale_price" autofocus>
 
@@ -126,15 +126,29 @@
                                             <option value="1">موجود</option>
                                             <option value="0">ناموجود</option>
                                         </select>
-
-
+{{--                                        <div class="form-group">--}}
+{{--                                            <label for="categories[]">{{ __(' دسته بندی') }}</label><i class="required">&nbsp; *</i>--}}
+{{--                                            <select id="categories" type="text"--}}
+{{--                                                    class="form-control @error('categories[]') is-invalid @enderror" name="categories[]"--}}
+{{--                                                    value="{{ old('categories[]') }}" required autocomplete="categories[]">--}}
+{{--                                                @foreach($categories as $cat)--}}
+{{--                                                    <option value="{{$cat->id}}">{{$cat->title}}</option>--}}
+{{--                                                @endforeach--}}
+{{--                                            </select>--}}
+
+
+{{--                                            @error('status')--}}
+{{--                                            <span class="invalid-feedback" role="alert">--}}
+{{--                                            <strong>{{ $message }}</strong>--}}
+{{--                                        </span>--}}
+{{--                                            @enderror--}}
+{{--                                        </div>--}}
                                         @error('status')
                                         <span class="invalid-feedback" role="alert">
                                             <strong>{{ $message }}</strong>
                                         </span>
                                         @enderror
                                     </div>
-
                                     <div class="form-group">
                                         <label for="discription">{{ __(' توضیحات  محصول') }}</label></label><i class="required">&nbsp; *</i>
                                         <textarea id="discription" type="text"

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

@@ -90,7 +90,7 @@
                                         <div class="form-group">
                                             <label for="price">  {{ __('قیمت  ') }}</label><i class="required">&nbsp; *</i>
                                             <input id="price" type="text"
-                                                   class="form-control @error('price') is-invalid @enderror"
+                                                   class="form-control price @error('price') is-invalid @enderror"
                                                    name="price"
                                                    value="{{$product->price}}" required autocomplete="price" autofocus>
 
@@ -104,7 +104,7 @@
                                         <div class="form-group">
                                             <label for="sale_price">{{ __(' قیمت فروش ویژه') }}</label>
                                             <input id="sale_price" type="sale_price"
-                                                   class="form-control @error('sale_price') is-invalid @enderror"
+                                                   class="form-control price @error('sale_price') is-invalid @enderror"
                                                    name="sale_price"
                                                    value="{{$product->sale_price}}" required autocomplete="sale_price">
 

+ 1 - 1
resources/views/panel/layouts/sidebar.blade.php

@@ -21,7 +21,7 @@
             <ul class="treeview-menu">
                 <li><a class="treeview-item pl-3" href="{{ route('products.index') }}"><i class="icon fa fa-circle-o"></i> محصولات </a></li>
                 <li><a class="treeview-item pl-3" href="{{ route('products.create') }}"><i class="icon fa fa-circle-o"></i>ایجاد محصول</a></li>
-                <li><a class="treeview-item pl-3" href=""><i class="icon fa fa-circle-o"></i>دسته بندی محصول</a></li>
+                <li><a class="treeview-item pl-3" href="{{ route('categories.index') }}"><i class="icon fa fa-circle-o"></i>دسته بندی محصول</a></li>
 {{--                <li><a class="treeview-item pl-3" href="{{ route('categories.index') }}"><i class="icon fa fa-circle-o"></i>‌دسترسی‌ها</a></li>--}}
             </ul>
         </li>

+ 0 - 9
routes/packages/web.php

@@ -1,9 +0,0 @@
-<?php
-
-use Illuminate\Support\Facades\Route;
-
-
-//categories *****
-Route::resource('/categories', 'Packages\Categories\CategoryController');
-
-

+ 3 - 0
routes/panel/web.php

@@ -20,5 +20,8 @@ Route::namespace('Authorization')->group(function () {
 Route::resource('/account', 'Account\AccountController')
     ->only( 'edit', 'update');
 
+//categories *****
+
+Route::resource('/categories', 'Categories\CategoryController');