简体   繁体   中英

Laravel collection where clause

Is possible in Laravel apply where condition to a collection and the result of that put in another collection without change the first one?

Ex:

$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;

With $documenti I get the first collection, in the foreach I apply the where condition and put the result to $dettaglio collection, but the first one ( $documenti ) was also changed. Why? It's like shared object

You can certainly apply where clauses to Collections along with a good number of other methods including filtering and mapping. See the documentation here:

Collections where clauses

$collection = Item::all();

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

$filtered->all();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM