首页 > 解决方案 > 如何删除扩展类中的父静态方法

问题描述

这是我的模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Associate extends Model
{
   // some code       
}

在控制器中,我使用类似于此的模型

<?php

namespace App\Http\Controllers;

use App\Models\Associate;
use Illuminate\Http\Request;

class AssociatesController extends Controller
{
    protected $associate;

    public function __construct(Associate $associate)
    {
        $this->associate = $associate;
    }

    public function edit(Request $request, $id)
    {
        $associate = $this->associate->with('some-relation')->find($id);
        // other part of code
    }
}

当我不使用控制器edit方法进行测试时,phpunit我不能模拟with方法,因为它是Illuminate\Database\Eloquent\Model.
我的问题是有办法删除父类的某些方法吗?

标签: phplaravel

解决方案


来自 Laravel文档

static Builder|Model with(array|string $relations)
Being querying a model with eager loading.

来自 PHP文档

<?php
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Here comes Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
?>

上面的示例将输出:


推荐阅读