id; $grid->model()->where('agent_id', $agent_id); $grid->column('id')->sortable(); $grid->column('product.picture', '产品图片')->image(60, 60); $grid->column('product.title', '产品名称')->limit(15); $grid->column('product_id', '产品详情') ->display('查看') ->modal(function ($v) { $titles = ['供应商', '产品标题', '产品图片', '原价', '现价', '销量', '库存']; $pic = isset($this->product->picture) ? "product->picture}\" style=\"max-width:80px;max-height:200px;cursor:pointer\" class=\"img img-thumbnail\">" : ''; $data = [[ $this->product->supplier->name ?? '', $this->product->title ?? '', $pic, $this->product->original_price ?? '', $this->product->price ?? '', $this->product->sale ?? '', $this->product->stock ?? '', ]]; return Table::make($titles, $data); }); $grid->column('price'); $grid->column('original_price'); $grid->column('sale'); $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray(); $grid->column('channel_id', '频道') ->display(function ($modal) use ($channels) { $data = array_flip(explode(',', $this->channel_id)); return join(',',array_intersect_key($channels, $data)); }) ->limit(10); $grid->column('category.name', '分类')->label(); $grid->column('status') ->using(ProductStatus::array()) ->dot([ ProductStatus::ON_SALE => 'success', ProductStatus::UNAUDITED => '', ProductStatus::REFUSE => 'danger', ProductStatus::SOLD_OUT => 'warning', ], 'primary'); $grid->column('created_at'); $grid->column('updated_at'); $grid->filter(function (Grid\Filter $filter) { $filter->panel(); $filter->model()->where('agent_id', Admin::user()->id); $filter->equal('id')->width(2); $filter->like('product.title', '产品标题')->width(3); $filter->equal('status')->select(ProductStatus::array())->width(2); $options = Supplier::where('status', 1)->pluck('name', 'id')->toArray(); $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2); }); }); } /** * Make a show builder. * * @param mixed $id * * @return Show */ protected function detail($id) { return Show::make($id, new AgentProduct(['agent:id,name', 'product.supplier:id,name']), function (Show $show) { $show->field('id'); $show->field('agent_id'); $show->field('product_id'); $show->field('price'); $show->field('original_price'); $show->field('sale'); $show->field('channel_id'); $show->field('category_id'); $show->field('status'); $show->field('created_at'); $show->field('updated_at'); $show->html(Alert::make(null, '供应商产品详情')->info()); $show->field('product.id', '供应商产品ID'); $show->field('product.supplier.name'); $show->field('product.title'); $show->field('product.pictures')->image(80, 80); $show->field('product.original_price'); $show->field('product.price'); $show->field('product.sale'); $show->field('product.stock'); $show->field('product.created_at', '创建时间'); $show->field('product.updated_at', '更新时间'); }); } /** * Make a form builder. * * @return Form */ protected function form() { return Form::make(new AgentProduct(['product:id,title']), function (Form $form) { $agent_id = Admin::user()->id; $form->display('id'); $form->hidden('agent_id')->value($agent_id); $form->selectTable('product_id', '供应商产品') //multipleSelectTable ->title('选择产品') ->dialogWidth('80%;min-width:825px;') ->from(SelectProduct::make()) ->model(Product::class) ->required(); $form->text('price')->required(); $form->text('original_price')->required(); $form->text('sale')->default(0); $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id)); array_shift($options); $form->multipleSelect('channel_id') ->options($options); $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id)); array_shift($options); $form->select('category_id') ->options($options) ->required(); $form->select('status') ->options([ ProductStatus::ON_SALE => '上架', ProductStatus::SOLD_OUT => '下架', ]) ->default(ProductStatus::ON_SALE) ->required(); })->saving(function (Form $form) { $agent_id = Admin::user()->id; //判断供应商产品是否存在或下架 if (!Product::where(['id' => $form->product_id, 'status' => ProductStatus::ON_SALE])->exists()) { return $form->response()->error('供应商不存在该产品或已下架,不可销售'); } //不允许编辑的字段 $form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']); //处理特殊字段 $form->agent_id = $agent_id; $form->status = ($form->status == ProductStatus::ON_SALE) ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT; //判断是否重复 $where = [ ['agent_id', '=', $agent_id], ['product_id', '=', $form->product_id], ]; if ($form->isEditing()) { $where[] = ['id', '<>', $form->getKey()]; } $agent_product = $form->repository()->model()->withTrashed()->where($where)->first(); if ($agent_product) { //如果已经软删除了,解除软删除后再更新 if ($agent_product->deleted_at) { $agent_product->deleted_at = null; $form->deleteInput('sale'); //保留原来的销量 $agent_product->update($form->input()); return $form->response()->success('保存成功')->script('history.back();'); } return $form->response()->error('该产品已经存在,请勿重复发布'); } }); } }