模型自定义了方法

假如我在模型自定义了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 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 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);
        });

    }
}
2年前 评论
charming-xiaoxia (楼主) 2年前
忆往昔弹指间 (作者) 2年前
忆往昔弹指间 (作者) 2年前
讨论数量: 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');
2年前 评论
charming-xiaoxia (楼主) 2年前
畅畅 (作者) 2年前
猪猪

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

2年前 评论

可考虑使用宏指令来扩展,新建 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);
        });

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

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

2年前 评论

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

2年前 评论