一、Dcat Admin列表页增加列表切换功能**
第一步
增加tab选项卡。并且给每个选项卡设置链接,代码如下:
$grid->header(function () {
$tab = Tab::make();
$tab->addLink('所有客户', '?source_id=0',true);
$tab->addLink('我的客户', '?source_id=1');
$tab->addLink('分享给我', '?source_id=2');
return $tab;
});
看到上面的代码,大家应该知道什么意思了。通过浏览器的参数来判断选项卡和查询条件。
现在我们可以试着访问
http://网址/xxx?source_id=0
http://网址/xxx?source_id=1
http://网址/xxx?source_id=2
页面的确是重新渲染了。
然后现在我们只需要获取浏览器的参数,然后判断不同的查询条件就可以。代码如下
构造方法获取浏览器参数
public function __construct(Request $request)
{
$this->source_id = $request->source_id;
return $this;
}
然后在列表方法里使用
if ($this->source_id == 0) {
$grid->model()->where(查询条件1);
} elseif ($this->source_id == 2) {
$grid->model()->where(查询条件2);
} else {
$grid->model()->where(其他查询);
}
基本已经完成了。
我们还需要完善一下选项卡的默认显示选项,加个判断即可
$grid->header(function () {
$tab = Tab::make();
$tab->addLink('我的客户', '?source_id=1',$this->source_id==1 ? true : false);
$tab->addLink('分享给我', '?source_id=2',$this->source_id==2 ? true : false);
$tab->addLink('公海客户', '?source_id=3',$this->source_id==3 ? true : false);
return $tab;
});
二、Dcat Admin新增个人信息字段
把扩展中的管理员模型复制到models:
<?php
namespace App\Models;
use Dcat\Admin\Models\Administrator as AdminUser;
class Administrator extends AdminUser
{
public function timer()
{
return $this->belongsTo(Department::class);
}
}
新建迁移文件
php artisan make:migration add_userinfo_admin_users
迁移文件内容:
class AddUserinfoAdminUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('admin_users',function (Blueprint $table) {
$table->char('mobile')->after('name')->nullable();
$table->char('qq')->after('name')->nullable();
$table->string('wechat')->after('name')->nullable();
$table->date('birthday')->after('name')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('admin_users', function (Blueprint $table) {
$table->dropColumn('mobile');
$table->dropColumn('qq');
$table->dropColumn('wechat');
$table->dropColumn('birthday');
});
}
}
执行迁移
php artisan migrate
找到以下文件
vendor\dcat\laravel-admin\src\Http\Controllers\AuthController.php
增加以下字段
$form->mobile('mobile', trans('admin.mobile'))->required();
$form->text('qq', trans('admin.qq'));
$form->text('wechat', trans('admin.wechat'));
$form->date('birthday', trans('admin.birthday'));
三、让Laravel应用默认直接访问Dcat Admin的办法
找到routes\web.php修改为
Route::redirect('/', '/admin');
s://wyz.xyz/d/71-dact-admin-100)