Laravel通过使用多个对象嵌套foreach [英] Laravel nested foreach by using multiple objects

查看:59
本文介绍了Laravel通过使用多个对象嵌套foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use some data in profile page that I successfully can pass those data to the view

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

但是当我想对每个嵌套使用基于数据"部分的"sub"数据打印到foreach时,我得到了一个错误

But when I want to use nested for each to print "sub" data in to the foreach base on the "data" section I got an error

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

我想做的最简单的事情

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

我在laravel文档上找到了它,但这不起作用

I find this on the laravel Docs but it doesn't work

    @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?

How we can able to use a foreach in side the other foreach?

注意:我传递给查看的所有数据都是对象. 我的子数据是

NOTE : all data that I pass to view are Object. My sub data is

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

推荐答案

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

Your problem is you are giving the same name in foreach loop.

您可以按照自己的代码进行操作,

You can do as per your code by doing this,

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

但这不是一个好习惯,

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

Based on my opinion, you have to change your variable's name in your controller, like you can pass datas instead of data, subs instead of sub,

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

然后您可以执行foreach循环

And then you can do foreach loop,

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

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

Always give your variable names relevant to your variable's work (singular/plural).

尝试此方法,如果有任何疑问,请发表评论.

Try this and comment if you have any doubts.

这篇关于Laravel通过使用多个对象嵌套foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆