柔晶美网络工作室

柔晶美网络工作室,倾心于web技术的博客站点

关注我 微信公众号

您现在的位置是: 首页 > 博客日记

Laravel-admin删除行操作回调无法获得模型id的解决办法

2019-10-24 admin laravel  2706

在phpexcel查询系统中,使用了bgsx存储各项目的基本属性,bm对就了项目的具体数据表。后台使用了Laravel-admin,遇到了一个问题是,删除bgsx这一行时,需要同时删除对应的bm的数据表。也就是说,删除行时,需要获得该行的模型id或bm,官方文档方法:

//删除后回调获取bm,再删除对应表
$form->deleted(function () {
$bm = $this->row->bm;//当前行bm
Schema::dropIfExists($bm);//删除bm对应的数据表
});

经测试无法获取到模型数据。又从github一个网友的方法里找到代码:

$form->deleting(function () {
	$url = Input::url();//通过链接地址获取id
	$arr = explode("/", $url);
	$id = end($arr);
	});

测试依然无效。

百度一下午,资料实在太少,只找到这一方法:

        public static function boot()
    {
        parent::boot();
        static::deleted(function ($model)
        {
           //这样可以拿到当前操作id
           dd($model->id);
        });
    }

再次测试,删除操作无数次,依然是删除成功却无法回调得到数据。晚上,认真阅读源码,最后直接修改源码实现了该过程。文件路径:

/vendor/encore/laravel-admin/src/Grid/Actions/Delete.php

原代码:

namespace Encore\Admin\Grid\Actions;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
class Delete extends RowAction
{
    /**
     * @return array|null|string
     */
    public function name()
    {
        return __('admin.delete');
    }
    /**
     * @param Model $model
     *
     * @return Response
     */
    public function handle(Model $model)
    {
        $trans = [
            'failed'    => trans('admin.delete_failed'),
            'succeeded' => trans('admin.delete_succeeded'),
        ];
        try {
            $model->delete();
        } catch (\Exception $exception) {
            return $this->response()->error("{$trans['failed']} : {$exception->getMessage()}");
        }
        return $this->response()->success($trans['succeeded'])->refresh();
    }
    /**
     * @return void
     */
    public function dialog()
    {
        $this->question(trans('admin.delete_confirm'), '', ['confirmButtonColor' => '#d33']);
    }
}

修改后的代码:

namespace Encore\Admin\Grid\Actions;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
class Delete extends RowAction
{
    /**
     * @return array|null|string
     */
    public function name()
    {
        return __('admin.delete');
    }
    /**
     * @param Model $model
     *
     * @return Response
     */
    public function handle(Model $model)
    {
        $trans = [
            'failed'    => trans('admin.delete_failed'),
            'succeeded' => trans('admin.delete_succeeded'),
        ];
        try {
        if (isset($model->bm)){
        $bm = $model->bm;//查询项目的表名
        Schema::dropIfExists($bm);//先删除项目表名,再删除bgsx这行
        }
        if (isset($model->cjdb)){
        $bm = $model->cjdb;//删除成绩对比表
        Schema::dropIfExists($bm);
        }
            $model->delete();
        } catch (\Exception $exception) {
            return $this->response()->error("{$trans['failed']} : {$exception->getMessage()}");
        }
        return $this->response()->success($trans['succeeded'])->refresh();
    }
    /**
     * @return void
     */
    public function dialog()
    {
        $this->question(trans('admin.delete_confirm'), '', ['confirmButtonColor' => '#d33']);
    }
}

2020年1月9日,官方文档已更新,删除回调方法已在1.7.0以上版本去除了,因此需要在model里使用模型删除回调,来获取该行的数据。修改bgsxModel,最终完美的实现:

use Illuminate\Support\Facades\Schema;
    //删除回调
public static function boot()
{
parent::boot();
static::deleted(function ($model)
{
$bm = $model->bm;//当前行bm
Schema::dropIfExists($bm);//删除bm对应的数据表
});
}

模型删除回调解决了laravel-admin删除回调问题,也避免了第一种方法,直接改源码导致更新后又要修改的不足。

文章评论


需要 登录 才能发表评论
热门评论
0条评论

暂时没有评论!