Laravel コレクションのメソッドはコレクションを変更しますか?Laravel 'コレクションメソッドがコレクションを変更する場所
ほぼすべてのメソッドは、コレクション
そして持っている唯一の方法の元のコピーを保存することができ、新しいコレクションのインスタンスを返します。Laravel documentationあなたがこれを読むことができるオン
コレクションの変更に関する警告はtransform
とforget
ですが、私はこのコードを持っています:
$descriptions = Description::where('description', $request->description);
if ($descriptions->count()) {
$descriptionsWithSameUnit = $descriptions->where('unit_id', $request->unit);
if ($descriptionsWithSameUnit->count()==0) {
$descriptionsWithAnotherUnit = $descriptions->where('unit_id', '!=', $request->unit);
if ($descriptionsWithAnotherUnit->count()) {
...
$descriptionsWithAnotherUnit
の後にコレクションが変更されるのは、その時点でコレクションには、unit_id == $request->unit
のレコードしかないためです。これはフレームワークやドキュメントのバグですか?
ここからの質問元のオブジェクトのコピーをデータベースから再度取得せずに保持するにはどうすればよいですか?私はこれを試してみました:
$descriptions = Description::where('description', $request->description);
if ($descriptions->count()) {
$descriptionsWithSameUnit = $descriptions;
$descriptionsWithSameUnit->where('unit_id', $request->unit);
...
しかし、私は$descriptionsWithSameUnit
オブジェクト
私は理解しています、私はEloquent whereメソッドを使用しています。コレクションはありません。 Thks、私はそれを見ていませんでした。 –
@MariaVilaró問題ありません。あなたが見ているそれらの方法は、Eloquentと同じではありません –