加入选择Laravel

时间:2018-02-16 13:43:51

标签: php laravel eloquent

我有这两个表:属性和照片。 1个属性可以有0或N张照片。

$properties = [
[id => 1,
photos = [
    id => 101,
    id => 102]],
[id => 2,
photos = [
    id => 201,
    id => 202]],
]

有没有办法使用Eloquent从DB中获取所有属性及其照片作为数组/类似对象的属性?

这样的事情:

db.collection ( "cities"). document ( "DC")
        .Delete ()
        .addOnSuccessListener (new OnSuccessListener <Void> () {
            @Override
            public void onSuccess (Void aVoid) {
                Log.d (TAG, "DocumentSnapshot successfully deleted!");
            }
        })
        .addOnFailureListener (new OnFailureListener () {
            @Override
            public void onFailure (@NonNull Exception e) {
                Log.w (TAG, "Error deleting document", e);
            }
        });

1 个答案:

答案 0 :(得分:2)

Property模型中创建public function photos() { return $this->hasMany(Photo::class, 'property_id', 'property_id'); } 关系:

$properties = Property::with('photos')->get();

加载数据并将其传递给视图:

@foreach ($properties as $property)
    {{ $property->property_id }}
    @foreach ($property->photos as $photo)
        {{ $photo->photo_id }}
    @endforeach
@endforeach

显示数据:

property_id

此外,我的回购邮件中描述的follow Laravel naming conventions是个好主意,并在id表格中将properties重命名为public function photos() { return $this->hasMany(Photo::class); } 。然后这种关系看起来像:

Private Sub easy()

    Dim ticker As String
    Dim vol As Double
    vol = 0

    Dim Summary_Table_Row As Integer
    Dim year_open As Double
    Dim year_close As Double

    Cells(1, 9).Value = "ticker"
    Cells(1, 10).Value = "Yearly_change"
    Cells(1, 12).Value = "Total Stock Vol"
    Cells(1, 11).Value = "Yearly_percentage"

    Summary_Table_Row = 2

    For i = 2 To 797712

      If year_open = 0 Then

          year_open = Cells(i, 3).Value
      End If

      If Cells(i - 1, 1) = Cells(i, 1) And Cells(i + 1, 1).Value <> Cells(i, 1).Value Then
          year_close = Cells(i, 6).Value
          yearly_change = year_close - year_open


          ticker = Cells(i, 1).Value


          vol = vol + Cells(i, 7).Value



          Range("j" & Summary_Table_Row).Value = yearly_change


          Range("I" & Summary_Table_Row).Value = ticker

          Range("K" & Summary_Table_Row).Value = year_percent



          Range("L" & Summary_Table_Row).Value = vol

          Summary_Table_Row = Summary_Table_Row + 1

          vol = 0


      Else

          vol = vol + Cells(i, 7).Value


      End If


    Next i

End Sub