模型自定义了方法

假如我在模型自定义了2个方法
 class BaseModel extends Model implements CacheableInterface
{
    use Cacheable;
    public        $timestamps = false;//关闭自动写入时间戳
    public string $alias      = '';
    public function withSearch(array $data, array $where = [], array $ignore = ['all'])
    {
      $builder= $this->xxxx();
        return $builder;
    }

    public function alias($alias)
    {
        $this->alias = $alias;
        return $this->from($this->getTable() . ' as ' . $alias);
    }
}
$a = (new mode())->alias('a')->withSearch();

这个时候就会提示Builder找不到withSearch,所以我改成了

假如我在模型自定义了2个方法
 class BaseModel extends Model implements CacheableInterface
{
    use Cacheable;
    public        $timestamps = false;//关闭自动写入时间戳
    public string $alias      = '';
    public function withSearch(array $data, array $where = [], array $ignore = ['all'])
    {
      $builder= $this->xxxx();
        return $builder;
    }

    public function alias($alias)
    {
        $this->alias = $alias;
         $this->from($this->getTable() . ' as ' . $alias);
        return $this;
    }
}
$a = (new mode())->alias('a')->withSearch();

这样的话from就无法生效了

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

可考虑使用宏指令来扩展,新建 App\Providers\MacrosServiceProvider::class,记得添加到config/app.phpproviders加载。

<?php

namespace App\Providers;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\ServiceProvider;

/**
 * 自定义宏指令
 *
 * @author brad <brader.wen@gmail.com>
 * @date 2022-03-28
 */
class MacrosServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        /**
         * findInSet() 方法   使用方法:query链式调用
         *
         * @param  string  $column 字段
         * @param  string  $value 要查询的值
         * @param  string  $boolean
         * @return \Illuminate\Database\Query\Builder
         */
        Builder::macro('findInSet', function ($column, $value, $boolean = 'and') {
            /** @var Builder $this */
            return $this->whereRaw("FIND_IN_SET(?, `$column`)", $value, $boolean);
        });

    }
}
1年前 评论
charming-xiaoxia (楼主) 1年前
忆往昔弹指间 (作者) 1年前
忆往昔弹指间 (作者) 1年前
讨论数量: 10

因为 from 后是另一个 class,建议换个写法

public function withSearch($alias, array $data, array $where = [], array $ignore = ['all'])
{
    $alias = $this->from($this->getTable() . ' as ' . $alias);
    $builder= $alias->xxxx();
    return $builder;
}

$a  =  (new  mode())->withSearch('a');
1年前 评论
charming-xiaoxia (楼主) 1年前
畅畅 (作者) 1年前
猪猪

public function alias 定义成 public static function alias 试试

1年前 评论

可考虑使用宏指令来扩展,新建 App\Providers\MacrosServiceProvider::class,记得添加到config/app.phpproviders加载。

<?php

namespace App\Providers;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\ServiceProvider;

/**
 * 自定义宏指令
 *
 * @author brad <brader.wen@gmail.com>
 * @date 2022-03-28
 */
class MacrosServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        /**
         * findInSet() 方法   使用方法:query链式调用
         *
         * @param  string  $column 字段
         * @param  string  $value 要查询的值
         * @param  string  $boolean
         * @return \Illuminate\Database\Query\Builder
         */
        Builder::macro('findInSet', function ($column, $value, $boolean = 'and') {
            /** @var Builder $this */
            return $this->whereRaw("FIND_IN_SET(?, `$column`)", $value, $boolean);
        });

    }
}
1年前 评论
charming-xiaoxia (楼主) 1年前
忆往昔弹指间 (作者) 1年前
忆往昔弹指间 (作者) 1年前

虽然不是我需要的但是给我一个思路了

1年前 评论

不对吧from是一个builder的函数 , new Model出来应该是一个model类吧

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!