Laravel-admin: 重写了model类下边的paginate,结果使用表单的filter功能的时候 ,点击搜索没有走自定义的方法

Created on 28 Aug 2018  ·  17Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: 5.5.30
  • PHP Version: 7.0.23
  • Laravel-admin: 1.5.19

Description:

表单的数据来源用的是自定义的数据,也就是重写了model类下边的paginate方法,可是用filter功能的时候,我看sql没有走自定义的方法,而是走了Eloquent ORM类的方法。这个是个大问题啊,而且我想把搜索条件里的字段处理下,请问我需要调用那个方法?有入口吗?
在线等....

Steps To Reproduce:

All 17 comments

走了Eloquent ORM类的哪个方法,你就重写哪个方法

不好意思,不是很明白。
public function paginate(){
$perPage = (int)Request::get('per_page', 20);

    $page = (int)Request::get('page', 1);

    $start = ($page-1)*$perPage;
    $channel = Admin::user()->channel;
    $query = DB::table('daily_record')
    ....

}
我是按照文档里边的 表格数据源 修改的这个方法,现在是页面中点击搜索之后,没走这个paginate()方法。

/admin/stats/channel
/admin/stats/channel?_pjax=%23pjax-container&date%5Bstart%5D=&date%5Bend%5D=&channel_p=cj
就是这1个方法,加了搜索条件之后,走了不同的分页策略

试试重写where方法

这么做岂不是越来越复杂了。。。失去了意义啊 。

那请问怎么解决,重写where是可以,但不知道怎么重写 。。。额

在线等..

同问这个问题

上边的方法是不对的,每次调用都会new Builder,所以where之后想再调用其他的方法,只能在原来的方法返回值上调用,而方法链是在

$this->queries->unique()->each(function ($query) {
    $this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
});

这里调用的,如果有filter的话,相当于先调用了where,那么paginate方法调用的只能是已经实例化的builder对象的。
所以只有重写where方法,来实现两个自定义方法都调用,简单实现如下:

    public function where()
    {
        $this->query = parent::where(...func_get_args());
        return $this;  // 返回自定义的模型给方法链中的$this->model
    }
    public function paginate()
    {
        dd(request()->input('id'));
        $this->query->paginate();
        ...
    }

访问?id=123时,先调用where再调用paginate

上边的方法是不对的,每次调用都会new Builder,所以where之后想再调用其他的方法,只能在原来的方法返回值上调用,而方法链是在

$this->queries->unique()->each(function ($query) {
    $this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
});

这里调用的,如果有filter的话,相当于先调用了where,那么paginate方法调用的只能是已经实例化的builder对象的。
所以只有重写where方法,来实现两个自定义方法都调用,简单实现如下:

    public function where()
    {
        $this->query = parent::where(...func_get_args());
        return $this;  // 返回自定义的模型给方法链中的$this->model
    }
    public function paginate()
    {
        dd(request()->input('id'));
        $this->query->paginate();
        ...
    }

访问?id=123时,先调用where再调用paginate

上边的方法是不对的,每次调用都会new Builder,所以where之后想再调用其他的方法,只能在原来的方法返回值上调用,而方法链是在

$this->queries->unique()->each(function ($query) {
    $this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
});

这里调用的,如果有filter的话,相当于先调用了where,那么paginate方法调用的只能是已经实例化的builder对象的。
所以只有重写where方法,来实现两个自定义方法都调用,简单实现如下:

    public function where()
    {
        $this->query = parent::where(...func_get_args());
        return $this;  // 返回自定义的模型给方法链中的$this->model
    }
    public function paginate()
    {
        dd(request()->input('id'));
        $this->query->paginate();
        ...
    }

访问?id=123时,先调用where再调用paginate

可以举个例子吗

上边的方法是不对的,每次调用都会new Builder,所以where之后想再调用其他的方法,只能在原来的方法返回值上调用,而方法链是在

$this->queries->unique()->each(function ($query) {
    $this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
});

这里调用的,如果有filter的话,相当于先调用了where,那么paginate方法调用的只能是已经实例化的builder对象的。
所以只有重写where方法,来实现两个自定义方法都调用,简单实现如下:

    public function where()
    {
        $this->query = parent::where(...func_get_args());
        return $this;  // 返回自定义的模型给方法链中的$this->model
    }
    public function paginate()
    {
        dd(request()->input('id'));
        $this->query->paginate();
        ...
    }

访问?id=123时,先调用where再调用paginate

  • Laravel Version: 5.5.30
  • PHP Version: 7.0.23
  • Laravel-admin: 1.5.19

Description:

表单的数据来源用的是自定义的数据,也就是重写了model类下边的paginate方法,可是用filter功能的时候,我看sql没有走自定义的方法,而是走了Eloquent ORM类的方法。这个是个大问题啊,而且我想把搜索条件里的字段处理下,请问我需要调用那个方法?有入口吗?
在线等....

Steps To Reproduce:

老哥 你的解决了吗

重定义了 paginate,想使用 Grid 的 Filter 的,可以用 layoutOnlyFilter

重定义了 paginate,想使用 Grid 的 Filter 的,可以用 layoutOnlyFilter
是这样加吗? 加了也不生效啊

` $grid->filter(function($filter){
// 去掉默认的id过滤器
$filter->disableIdFilter();
// 在这里添加字段过滤器
$filter->layoutOnly()->like('id', '注单号');

    });`

这是我重写的paginate 是复杂sql出来的

` public function paginate()
{
$perPage = Request::get('per_page', 10);

    //返回联合查询数据
    $lists=$this->getUnionData();

    $total=count($lists);

    $movies = static::hydrate($lists);

    $paginator = new LengthAwarePaginator($movies, $total, $perPage);

    $paginator->setPath(url()->current());

    return $paginator;
}`

重定义了 paginate,想使用 Grid 的 Filter 的,可以用 layoutOnlyFilter

因为我的表是分表,模型没用对应的边,所有
$grid->model() ->when(!request('id'), function ($query) { return $query->where('id', '>', 10); });
加了这段 会报错表不存在

layoutOnly 就只是界面上会显示这个筛选器,只是 post 把参数传到后端,不会在 $grid->model 上加 where 过滤,获取到参数值后,在哪使用都可以。像你的例子,直接在 paginate 里 获取 $id = request('id'); 然后使用就行了。

layoutOnly 就只是界面上会显示这个筛选器,只是 post 把参数传到后端,不会在 $grid->model 上加 where 过滤,获取到参数值后,在哪使用都可以。像你的例子,直接在 paginate 里 获取 $id = request('id'); 然后使用就行了。
感谢 解决了

上边的方法是不对的,每次调用都会new Builder,所以where之后想再调用其他的方法,只能在原来的方法返回值上调用,而方法链是在

$this->queries->unique()->each(function ($query) {
    $this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
});

这里调用的,如果有filter的话,相当于先调用了where,那么paginate方法调用的只能是已经实例化的builder对象的。
所以只有重写where方法,来实现两个自定义方法都调用,简单实现如下:

    public function where()
    {
        $this->query = parent::where(...func_get_args());
        return $this;  // 返回自定义的模型给方法链中的$this->model
    }
    public function paginate()
    {
        dd(request()->input('id'));
        $this->query->paginate();
        ...
    }

访问?id=123时,先调用where再调用paginate

然后你告诉我,为何就出现了不可调用静态方法这样的错?
Non-static method App\Models\Ebusiness\EbusinessOrder::where() should not be called statically

当我加入

    public static function __callStatic($funcname, $arguments)
    {
        echo "我是魔术呀";
        $app = new self(...$arguments);
        return $app;
    }

发现不执行,没用
当我去掉static时如下

    public  function __callStatic($funcname, $arguments)
    {
        echo "我是魔术呀";
        $app = new self(...$arguments);
        return $app;
    }

就发生以下错

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot make static method Illuminate\Database\Eloquent\Model::__callStatic() non static in class App\Models\Ebusiness\EbusinessOrder
Was this page helpful?
0 / 5 - 0 ratings

Related issues

donglianyou picture donglianyou  ·  3Comments

benny-sun picture benny-sun  ·  3Comments

evans-kim picture evans-kim  ·  3Comments

xiaalngf picture xiaalngf  ·  3Comments

clock1129 picture clock1129  ·  3Comments