示例#1
0
 public function index()
 {
     $userCount = User::count();
     $productCount = Product::count();
     $fieldCount = Field::count();
     $lastUsers = User::take(5)->get();
     $lastProducts = Product::take(5)->get();
     $lastFields = Field::take(5)->get();
     return view('backend.index', compact('userCount', 'categoryCount', 'productCount', 'lastUsers', 'lastProducts', 'fieldCount', 'lastFields'));
 }
 /**
  * Product store
  *
  * @param ShopProductStoreRequest $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function store(ShopProductStoreRequest $request)
 {
     $product = Product::create($request->only('name', 'content', 'price', 'active'));
     if ($request->has('photos')) {
         $photos = Photo::whereIn('id', $request->get('photos'))->get();
         $product->photos()->saveMany($photos);
     }
     if ($request->has('category_id')) {
         $product->categories()->attach($request->get('category_id'));
     }
     //@Todo check field before save
     $product->fields()->attach(array_keys($request->field));
     foreach ($request->get('field') as $fieldId => $value) {
         if (!empty($value)) {
             $field = Field::findOrFail($fieldId);
             FieldValue::create([$field->type => $value[$field->type], 'product_id' => $product->id, 'field_id' => $field->id]);
         }
     }
     if ($request->has('fields')) {
         $product->fields()->attach($request->get('fields'));
     }
     Session::flash('message', 'Товар добавлен');
     return redirect()->route('manager.shop.product.edit', $product->id);
 }
 public function storeOptionField(ShopFieldStoreRequest $request)
 {
     $field = Field::create($request->only(['name', 'content', 'category_id', 'type', 'option_id']));
     return response()->json(['status' => 'ok', 'field' => $field->toArray()]);
 }
 /**
  * Edit category form
  *
  * @param Category $category
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  */
 public function edit(Category $category)
 {
     $fields = Field::lists('name', 'id')->toArray();
     $categories = ['' => 'Родительская категория'] + Category::where('id', '!=', $category->id)->whereNotIn('id', $category->children()->lists('id')->toArray())->lists('name', 'id')->toArray();
     return view('backend.shop.category.edit', compact('category', 'categories', 'fields'));
 }