如何通过Laravel集合进行Foreach循环。

时间:2018-04-29 01:02:12

标签: php laravel laravel-5 eloquent

我是laravel的初学者。使用5.5。我的控制器回复了这个系列:

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

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

关系很好。但如何从集合中检索它?我不知道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

1 个答案:

答案 0 :(得分:0)

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

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

它会返回bazars的集合,然后你可以在那里运行循环。