首先要先创建数据表,例如:
CREATE TABLE `group` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`group_title` varchar(30) DEFAULT NULL COMMENT '标题',
`goods_id` int(10) DEFAULT NULL COMMENT '关联商品id',
`group_num` int(20) DEFAULT NULL COMMENT '拼团人数',
`group_price` decimal(15,2) DEFAULT NULL COMMENT '拼团价格',
`staus` varchar(50) DEFAULT NULL COMMENT '状态',
`starts_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '活动开始时间',
`end_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '活动结束时间',
`created_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`deleted_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '删除时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
- admin创建拼团模型层
php artisan make:model GroupModel
- 模型层
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class GroupModel extends Model
{
//绑定表名
protected $table ='group';
//use SoftDeletes;
}
- admin创建拼团页面控制器
php artisan admin:make GroupController --model=App\GroupModel
- 创建好控制器会自动生成路由
$router->resource('group-models', GroupController::class);
- 控制器
<?php
namespace App\Admin\Controllers;
use App\GroupModel;
use App\Repository\Interfaces\CInterfaces;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class GroupController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = 'GroupModel';
/**
* Make a grid builder.
*
* @return Grid
*/
protected $asd;
public function __construct(CInterfaces $asdf)
{
$this->asd = $asdf;
}
protected function grid()
{
$grid = new Grid(new GroupModel());
$grid->column('id', __('Id'));
$grid->column('group_title', __('标题'));
$grid->column('goods_id', __('商品id'));
$grid->column('group_num', __('拼团人数'));
$grid->column('group_price', __('拼团价格'));
$grid->column('starts_at', __('开始时间'));
$grid->column('end_at', __('结束时间'));
$grid->column('created_at', __('创建时间'));
$grid->column('updated_at', __('修改时间'));
$grid->column('deleted_at', __('删除时间'));
$grid->selector(function (Grid\Tools\Selector $selector) {
$selector->select('staus', '所有活动', [
0 => '所有拼团',
1 => '未开始',
2 => '进行中',
3 => '已结束',
4 => '已失效',
], function ($query, $value) {
$num = $value[0];
$a = $this->asd->Asdf($num);
//dd($value[0]);
// $query->whereBetween('status', $between[$value['0']]);
// $query->whereBetween('id', [1,3]);
$query->wherein('id', $a);
});
});
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(GroupModel::findOrFail($id));
$show->field('id', __('Id'));
$show->field('group_title', __('Group title'));
$show->field('goods_id', __('Goods id'));
$show->field('group_num', __('Group num'));
$show->field('group_price', __('Group price'));
$show->field('starts_at', __('Starts at'));
$show->field('end_at', __('End 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 GroupModel());
$form->text('group_title', __('Group title'));
$form->number('goods_id', __('Goods id'));
$form->number('group_num', __('Group num'));
$form->decimal('group_price', __('Group price'));
$form->datetime('starts_at', __('Starts at'))->default(date('Y-m-d H:i:s'));
$form->datetime('end_at', __('End at'))->default(date('Y-m-d H:i:s'));
$form->datetime('created_at', __('Created at'))->default(date('Y-m-d H:i:s'));
return $form;
}
}
- 新建仓库目录
https://blog.csdn.net/simplexingfupeng/article/details/82083395?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
https://blog.csdn.net/sinat_21125451/article/details/54290962?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-8.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-8.channel_param
<?php
App下的Providers 目录下的 RepositoryStusProvider.php
namespace App\Providers;
use App\Repository\Interfaces\CInterfaces;
use App\Repository\Repositories\CRe;
use Illuminate\Support\ServiceProvider;
class RepositoryStusProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
//绑定仓库
CInterfaces::class,
CRe::class
);
}
}
Interfaces
<?php
Repository目录下的 Interface下的 CInterfaces.php
namespace App\Repository\Interfaces;
Interface CInterfaces
{
public function Asdf($num);
}
<?php
Repository目录下的 Repositories下的 CRe.php
namespace App\Repository\Repositories;
use App\GroupModel;
use App\Repository\Interfaces\CInterfaces;
class CRe implements CInterfaces
{
public function Asdf($num)
{
$times = date("Y-m-d H:i:s");//获取当前时间
if ($num == 1) {//未开始
$ids = GroupModel::where('starts_at', '>', $times)
->where('staus', '=', 1)
->get()->pluck('id');
return $ids;
} elseif ($num == 0) {//所有活动
$ids = GroupModel::get()->pluck('id');
return $ids;
} elseif ($num == 2) {//正在进行的
$ids = GroupModel::where('starts_at', '<', $times)
->where('staus', '=', 1)
->where('end_at', '>', $times)
->get()->pluck('id');
// dd(132);
return $ids;
} elseif ($num == 3) {//结束的
$ids = GroupModel::where('end_at', '>', $times)
->where('staus', '=', 1)
->get()->pluck('id');
// dd(132);
return $ids;
} else {//失效的
$ids = GroupModel::where('staus', '=', 0)
->get()->pluck('id');
// dd(132);
return $ids;
}
}
}
在 config 目录下的app.php加入
App\Providers\RepositoryStusProvider::class,