首页 > 解决方案 > 为什么存储在变量(方法范围)中的 Laravel Builder 对象可以被同一类中的另一个方法修改?

问题描述

假设有一个名为 ReportType 的模型,其中包含一些字段(id、name、created_at)。

在我的控制器(ReportTypeController.php)

<?php

namespace App\Http\Controllers;

class HomeController extends Controller
{

    public function index()
    {
        $reportType = ReportType::whereNotNull('created_at');

        $this->addSomeConditions($reportType);

        return $reportType;
    }

    private function addSomeConditions($reportType)
    {
        $reportType->whereIn('id', [1, 2, 3]);
    }

}

$reportType索引中的变量将具有whereIn来自方法的条件,addSomeConditions即使$reportType不是通过引用传递并且该addSomeConditions方法没有任何返回值。类构建器是否正在实现单例,在这个类中只有一个对象是活动的?

标签: phplaraveloopeloquentbuilder

解决方案


推荐阅读