Laravel集合where子句

Min*_*ndo 0 php collections laravel

Laravel是否可以将条件应用于某个集合,并将其结果放入另一个集合而无需更改第一个集合?

例如:

$documenti = DB::table("RVW_DocumentiDettaglio")
                ->select("*")
                ->where("IDAzienda", "=", $request->session()->get("operatore.IDAzienda"))
                ->whereIn("CodiceStato", [Documento::E_DOC, Documento::W_DOC, Documento::OK_DOC])
                ->orderBy("IDDocumentoTestata", "asc");

// Ciclo le voci spesa
foreach ($voci_spesa as $voce_spesa) {
    Log_Controller::debug("Documenti:" . $documenti->get());

    if ($voce_spesa["Manuale"]) {
        $dettaglio = $documenti->where("Descrizione", "like", "%" . $voce_spesa["Descrizione"] . "%");
    } else {
        $dettaglio = $documenti->where("Descrizione", "=", $voce_spesa["Descrizione"]);
    }
    Log_Controller::debug("Dettaglio:" . $dettaglio->get());
}
die;
Run Code Online (Sandbox Code Playgroud)

随着$documenti我得到的第一个系列,在foreach我适用where语句,并把结果$dettaglio集,但第一个($documenti)也被改变了。为什么?就像共享对象

小智 6

当然,您可以将where子句与Collections一起应用,还可以使用许多其他方法,包括过滤和映射。请参阅此处的文档:

集合where子句

$collection = Item::all();

$filtered = $collection->where('price', 100);

$filtered->all();
Run Code Online (Sandbox Code Playgroud)