【发布时间】: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打印子
【问题讨论】:
-
您正在使用
$usersforeach。你有没有把它从控制器发送到视图? -
你从哪里得到
$loop变量?你确定这是你抛出错误的地方吗?在您提供的代码中,您似乎并未尝试在任何地方访问id属性。 -
@Nasser Ali Karimi 您需要对哪个对象进行嵌套 foreach?
-
@HirenGohel 我看到视图中的所有数据都可用,只是当我想为每个使用嵌套时出现错误
-
@domdambrogia 我看到我在 Laravel 文档中得到了对我不起作用的代码
标签: php arrays laravel foreach