【问题标题】:Laravel nested foreach by using multiple objectsLaravel 使用多个对象嵌套 foreach
【发布时间】:2018-09-19 14:06:34
【问题描述】:

我想在个人资料页面中使用一些数据,我可以成功地将这些数据传递给视图

    return view('pages.profile' , compact('data' , 'faculty' , 'scores' , 'sub' , 'finance_r'));

但是当我想使用 nested for each 将“子”数据打印到基于“数据”部分的 foreach 中时,我遇到了一个错误

Trying to get property 'id' of non-object (View: 

我想做的最简单的事情

@foreach ($data as $data)
  @foreach ($sub as $sub)
    {{ $sub->id }}
  @endforeach
@endforeach

我在 laravel Docs 上找到了这个,但它不起作用

    @foreach ($users as $user)
    @foreach ($user->posts as $post)
        @if ($loop->parent->first)
            This is first iteration of the parent loop.
        @endif
    @endforeach
@endforeach

我们如何才能在另一个 foreach 中使用一个 foreach?

注意:我传递给视图的所有数据都是对象。 我的子数据是

Collection {#865 ▼
  #items: array:8 [▼
    0 => Subject {#856 ▼
      #table: "subject"
      +timestamps: false
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:5 [▼
        "id" => 8
        "name" => "پروگرامینگ"
        "number_of_credites" => 5
        "semester_id" => 1
        "faculty_id" => 2
      ]
      #original: array:5 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
    1 => Subject {#857 ▶}
    2 => Subject {#858 ▶}

还有我的数据

    Collection {#848 ▼
  #items: array:3 [▼
    0 => Scores {#844 ▼
      #table: "scores"
      +timestampes: false
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 [▼
        "id" => 4
        "subject_id" => 15
        "score" => 100
        "scores_student_details_id" => 7
        "created_at" => "2018-04-09 15:02:24"
        "updated_at" => "2018-04-09 15:02:21"

我其实是想根据"faculty_id" => 1打印子

【问题讨论】:

  • 您正在使用$users foreach。你有没有把它从控制器发送到视图?
  • 你从哪里得到$loop 变量?你确定这是你抛出错误的地方吗?在您提供的代码中,您似乎并未尝试在任何地方访问 id 属性。
  • @Nasser Ali Karimi 您需要对哪个对象进行嵌套 foreach?
  • @HirenGohel 我看到视图中的所有数据都可用,只是当我想为每个使用嵌套时出现错误
  • @domdambrogia 我看到我在 Laravel 文档中得到了对我不起作用的代码

标签: php arrays laravel foreach


【解决方案1】:

您的问题是您在 foreach 循环中使用了相同的名称。

您可以按照您的代码执行此操作,

@foreach ($data as $value)
  @foreach ($sub as $subValue)
    {{ $subValue->id }}
  @endforeach
@endforeach

但这不是一个好习惯,

根据我的意见,您必须在控制器中更改变量的名称, 比如你可以通过datas而不是datasubs而不是sub

 return view('pages.profile' , compact('datas' , 'faculty' , 'scores' , 'subs' , 'finance_r'));

然后你就可以做foreach循环了,

@foreach ($datas as $data)
      @foreach ($subs as $sub)
        {{ $sub->id }}
      @endforeach
@endforeach

始终提供与变量工作相关的变量名称(单数/复数)。

如果您有任何疑问,请尝试此操作并发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2018-11-29
    • 2017-05-09
    • 2016-10-27
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多