數倉+admin拼團

守候2009發表於2020-09-03

ibrand_multi_groupon拼團表

CREATE TABLE `ibrand_multi_groupon` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` varchar(191) DEFAULT NULL COMMENT '拼團名稱',
  `goods_id` int(10) DEFAULT NULL,
  `nums` int(10) DEFAULT NULL COMMENT '拼團人數',
  `price` decimal(10,2) DEFAULT NULL COMMENT '價格',
  `sort` bigint(4) DEFAULT NULL COMMENT '排序',
  `status` tinyint(4) DEFAULT '1' COMMENT '狀態',
  `starts_at` datetime DEFAULT NULL COMMENT '開始時間',
  `ends_at` datetime DEFAULT NULL COMMENT '結束時間',
  `created_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;

商品表

CREATE TABLE `goods_db` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `goods_no` varchar(32) NOT NULL,
  `goods_name` varchar(100) DEFAULT NULL,
  `category_id` bigint(4) DEFAULT NULL,
  `sell_price` decimal(10,2) DEFAULT NULL,
  `img` varchar(100) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

建立對應模型層

php artisan make:model ....

建立admin控制器

php artisan admin:make Controller --model=App\User

admin規格選擇器
admin
數倉

php artisan make:provider RiakServiceProvider

數倉繫結

 public function register()
    {
        $this->app->bind(
            AsdInterfaces::class,
            AsdRepository::class
        );
    }

Repository資料夾
Interfaces Repositories
Interfaces內的介面

<?php


namespace App\Repository\Interfaces;


interface AsdInterfaces
{
    public function Asdf($num);
}

Repositories 介面實現方法

<?php


namespace App\Repository\Repositories;


use App\IbrandMultiGroupon;
use App\Repository\Interfaces\AsdInterfaces;

class AsdRepository implements AsdInterfaces
{

    public function Asdf($num)
    {/*  '0'=> '所有活動',
                1=> '未開始',
                2=>'進行中',
                3=>'已結束',
                4=>'已失效',*/

        $times=date("Y-m-d H:i:s");//獲取當前時間
        if ($num==1){//未開始
            $ids=IbrandMultiGroupon::where('starts_at','>',$times)
                ->where('status','=',1)
                ->get()->pluck('id');
           return $ids;
        }elseif ($num==0){//所有活動
            $ids=IbrandMultiGroupon::get()->pluck('id');
            return $ids;
        }elseif ($num==2){//正在進行的
            $ids=IbrandMultiGroupon::where('starts_at','<',$times)
                ->where('status','=',1)
                ->where('ends_at','>',$times)
                ->get()->pluck('id');
//            dd(132);
            return $ids;
        }elseif ($num==3){//結束的
            $ids=IbrandMultiGroupon::where('ends_at','<',$times)
                ->where('status','=',1)
                ->get()->pluck('id');
//            dd(132);
            return $ids;
        }else{//失效的
            $ids=IbrandMultiGroupon::where('status','=',0)
                ->get()->pluck('id');
//            dd(132);
            return $ids;
        }

    }
}


團購控制器

<?php

namespace App\Admin\Controllers;

use App\DbGoods;
use App\IbrandMultiGroupon;
use App\Repository\Interfaces\AsdInterfaces;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class IbrandMultiGrouponController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'IbrandMultiGroupon';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected $asd;

    public function __construct(AsdInterfaces $asdf)
    {
        $this->asd = $asdf;
    }

    protected function grid()
    {
        $grid = new Grid(new IbrandMultiGroupon());
        $a = 1;
        $grid->selector(function (Grid\Tools\Selector $selector) {
            $selector->select('status', '拼團狀態', [
                '0' => '所有活動',
                1 => '未開始',
                2 => '進行中',
                3 => '已結束',
                4 => '已失效',
            ], function ($query, $value) {

                $num = $value[0];
                $a = $this->asd->Asdf($num);
//dd($value[0]);
                $query->wherein('id', $a);


            }
            );
        });

        $grid->column('id', __('Id'));
        $grid->column('title', __('標題'));
        $grid->column('goods_id', __('商品編號'));
        $grid->column('nums', __('拼團人數'));
        $grid->column('price', __('拼團價格'));
        $grid->column('sort', __('排序'));
        $grid->column('status', __('狀態'))->editable('select',[
            0 => '下架',

            1 => '上架',
        ]);
        $grid->column('starts_at', __('開始時間'));
        $grid->column('ends_at', __('結束時間'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        $grid->column('deleted_at', __('Deleted at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(IbrandMultiGroupon::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('goods_id', __('商品編號'));
        $show->field('nums', __('拼團人數'));
        $show->field('price', __('拼團價格'));
        $show->field('sort', __('排序'));
        $show->field('status', __('狀態'));
        $show->field('starts_at', __('開始時間'));
        $show->field('ends_at', __('結束時間'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new IbrandMultiGroupon());

        $form->text('title', __('Title'));
//        $form->number('goods_id', __('商品編號'));
//        dd(DbGoods::get()->pluck('goods_name','id'));
        $form->radio('goods_id')->options(DbGoods::get()->pluck('goods_name', 'id'))->stacked();
        $form->number('nums', __('拼團人數'));
        $form->decimal('price', __('拼團價格'));
        $form->number('sort', __('排序'));
        $form->select('status', __('狀態'))->options([
            0 => '下架',

            1 => '上架',
        ])->default(1);
        $form->datetime('starts_at', __('開始時間'));//->default(date('Y-m-d H:i:s'))
        $form->datetime('ends_at', __('結束時間'));

        return $form;
    }
}

回收站軟刪除
請點選

控制器
   $grid->filter(function ($filter) {

            // 範圍過濾器,呼叫模型的`onlyTrashed`方法,查詢出被軟刪除的資料。
            $filter->scope('trashed', '回收站')->onlyTrashed();

        });

回收站

模型層
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class IbrandMultiGroupon extends Model
{
    protected $table = 'ibrand_multi_groupon';
    use SoftDeletes;

}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章