php - 为什么存储在变量(方法范围)中的 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
方法没有任何返回值。类构建器是否正在实现单例,在这个类中只有一个对象是活动的?
解决方案
推荐阅读
- python - 循环集成,循环可以三重嵌套吗?
- symfony - 交响乐:
在树枝文件中的字段之间添加 - c# - 如何对不同的 API 请求使用相同的属性
- java - 我已升级到 gradle 7.2,现在我的项目遇到错误
- python - 如何基于包含列名的列表创建数据框?
- c - 在不分配内存的情况下为指针赋值(C)?
- python - 冲突:被其他 getUpdates 请求终止;确保只有一个机器人实例正在运行
- rust - 为什么 mpsc::Sender#send 和 mpsc::Receiver#recv 没有被标记为 mut?
- javascript - 弹出通知面板框不在右侧
- elasticsearch - 嵌套弹性搜索文档上的不同值