【问题标题】:How to Foreach loop through Laravel collection.?如何通过 Laravel 集合进行 Foreach 循环。?
【发布时间】:2018-10-09 11:34:37
【问题描述】:

我是 laravel 的初学者。使用 5.5 。我的控制器返回了这个集合:

$products = Product::with('bazar.resellers')->take(2)->get();
dd($products);
//return view('shop.index')->with('products', $products);

这基本上是三个具有嵌套关系的表。请注意下面代码中的 relations 列表。我想从集合中的每个模型(即产品、集市和经销商)访问数据(所有红色列)。

关系很好。但是如何从集合中检索它?我不知道 foreach 循环如何处理集合。

Collection {#578 ▼
#items: array:2 [▼
0 => Product {#487 ▼
  #fillable: array:7 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "id" => 1
    "created_at" => "2018-04-27 12:54:41"
    "updated_at" => "2018-04-27 12:54:41"
    "imgp" => "shirt.jpg"
    "title" => "shirt1"
    "Prod_descript" => "shirt1 is a good shirt"
    "price" => 10
    "reseller_id" => 1
    "City_id" => 1
    "bazar_id" => 1
  ]
  #original: array:10 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "bazar" => Bazar {#523 ▼
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 1
        "Bazarname" => "Saddar"
        "Bazarlat" => null
        "Bazarlong" => null
        "created_at" => null
        "updated_at" => null
        "City_id" => 1
      ]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "resellers" => Collection {#574 ▼
          #items: array:1 [▼
            0 => Reseller {#564 ▼
              #guard: "reseller"
              #fillable: array:12 [▶]
              #hidden: array:2 [▶]
              #connection: "mysql"
              #table: null
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:15 [▼
                "id" => 1
                "Fname" => "talha"
                "Lname" => "ali"
                "email" => "p@g.com"
                "password" => "$2y$10$M7wOmSouxSAYADN7NeFdSObT8fGwkEOFxVmOgcNSWvrixWCDtA/1S"
                "mobile_no" => "03169880008"
                "landline_no" => "0987654"
                "shop_name" => "MyTestSHop"
                "NIC_no" => "170178359437589754"
                "shop_address" => "University Town, Peshawar, Pakistan"
                "remember_token" => "wrb3nOwOWQcTuoF0P9KnaknwpOxfpTHt4gkShUmTzkR2Df9A7pPY5shBq6pQ"
                "created_at" => "2018-04-27 12:42:25"
                "updated_at" => "2018-04-27 12:42:25"
                "City_id" => 1
                "bazar_id" => 1
              ]
              #original: array:15 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #visible: []
              #guarded: array:1 [▶]
              #rememberTokenName: "remember_token"
            }
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}
1 => Product {#488 ▶}
]
}

我试过的代码不起作用!

@foreach($products as $product)
<h1>{{ $product->title }}</h1>

@foreach($product->bazar as $bazar)
    <h3>{{ $bazar->Bazarname }}</h3>

    @foreach($bazar->resellers as $reseller)
        <p>{{ $reseller->Fname }}</p>
    @endforeach

@endforeach

@endforeach

【问题讨论】:

  • $product-&gt;bazar 是单个对象,而不是集合。
  • 你应该注意你得到的错误。
  • @JonasStaudenmeir 所以集合是针对多个对象的?我还可以使用什么其他功能来获取表格的前两行?除了 get () 吗?因为它总是返回一个集合。
  • @SaidbakR 它给出了一个空白页。因为我不理解收集和循环的概念。文档对我帮助不大。我的英语很弱。谁能用简单的话告诉我这个收藏品是做什么用的?
  • 集合只是 Laravel 版本的数组。你的收藏处理很好。我的意思是$product-&gt;bazar 不是一个集合,所以你必须使用$product-&gt;bazar-&gt;Bazarname@foreach($product-&gt;bazar-&gt;resellers

标签: php laravel laravel-5 eloquent


【解决方案1】:

如果产品有单一市场,那么您不需要在$product-&gt;bazar 上使用 foreach。你只需要直接使用 bazar 属性。如果产品有很多市场,则更新您的产品型号bazar() 方法,如:

public function bazars()
{
    return $this->hasMany(Bazar::class);
}

它返回集市集,然后你可以在上面运行循环。

【讨论】:

  • 感谢兄弟的帮助。产品型号belongsto(App\Reseller) belongsto(App\City) belongsto(App\Bazar) 但是当我做$product-&gt;bazar-&gt;Bazarname; 它返回错误尝试
  • 这个产品有bazar吗? {{ dd($product-&gt;bazar) }} 的结果是什么?
  • @JonasStaudenmeir 感谢您的帮助兄弟。是的,这个产品属于一个集市。如果我进行查询,即$products = Product::with('bazar.resellers')-&gt;take(2)-&gt;get();,然后是foreach ($products as $product) { dd($product-&gt;bazar); },那么它会返回一大堆结果。包括那个集市模型。
  • $product-&gt;bazar-&gt;Bazarname 给你什么错误?
猜你喜欢
  • 2018-06-20
  • 2018-11-18
  • 1970-01-01
  • 2016-12-23
  • 2020-05-23
  • 2019-08-21
  • 2018-05-13
  • 2013-01-20
  • 2017-01-27
相关资源
最近更新 更多