****更新*******************************************************
我不知道为什么,但将所有内容移到controllers index()方法而不是show()方法解决了问题。我不知道为什么它会起作用,但它确实起作用了。
原始问题:
我想不出这里出了什么问题,我已经按照文档进行了操作,尝试了变体等。。。仍然无法正确执行此操作。
型号A:(幻灯片)
public function carousels() {
return $this->belongsToMany(Carousel::class)->withTimestamp()
}
B型:(旋转木马)
public function slides() {
return $this->belongsToMany(Slide::class)->withTimestamp()
}
数据透视表
--------------------------------------------------
|carousel_id | slide_id | created_at | updated_at|
--------------------------------------------------
来自控制器:
public function show(Carosuel $carousels) {
$carousels = $carousel->with('slides)->get();
return view('myView', compact(['carousels']));
}
在以下转储中,“幻灯片”不在集合的attributes对象中;然而,它们是在关系对象中。我不确定这是正常行为还是幻灯片数组应该在attributes对象中。如果是后者,那么如何才能做到这一点呢?
$caousels转储:
Collection {#1320 â¼
#items: array:20 [â¼
0 => Carousel {#798 â¼
#fillable: array:4 [â¶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [â¼
"id" => 1
"name" => "Prof. Andre Gerlach"
"category" => "Dr."
"active" => 0
"deleted_at" => null
"created_at" => "2018-04-07 21:10:52"
"updated_at" => "2018-04-07 21:10:52"
]
#original: array:7 [â¶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [â¼
"slides" => Collection {#1300 â¼
#items: array:5 [â¼
0 => Slide {#1023 â¶}
1 => Slide {#1024 â¶}
2 => Slide {#1025 â¶}
3 => Slide {#1026 â¶}
4 => Slide {#1027 â¶}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [â¶]
}
1 => Carousel {#799 â¶}
2 => Carousel {#800 â¶}
在视图中:
foreach($carousels as $carousel){
echo $carousel->name;
foreach($carousel->slides as $slide){
echo $slide->tag;
}
}
这引发了
E_ERROR Invalid argument supplied for foreach() in view
但是,如果我将其强制转换为数组,它就会起作用。问题是我还需要对结果分页。一旦我这样做了,幻灯片阵列将再次无法访问。
public function show(Carosuel $carousels) {
// This works - slides are available to me in the view
$carousels = $carousel->with('slides)->get()->toarray();
// This DOESN'T work - slides are not available to me in the view
$carousels = $carousel->with('slides)->paginate(6)->toarray();
return view('myView', compact(['carousels']));
}
转储为阵列:
array:12 [â¼
"current_page" => 1
"data" => array:6 [â¼
0 => array:8 [â¼
"id" => 1
"name" => "Prof. Andre Gerlach"
"category" => "Dr."
"active" => 0
"deleted_at" => null
"created_at" => "Apr 07 2018"
"updated_at" => "2018-04-07 21:10:52"
"slides" => array:5 [â¼
0 => array:11 [â¶]
1 => array:11 [â¶]
2 => array:11 [â¶]
3 => array:11 [â¶]
4 => array:11 [â¶]
]
]
1 => array:8 [â¶]
2 => array:8 [â¶]
3 => array:8 [â¶]
4 => array:8 [â¶]
5 => array:8 [â¶]
]
"first_page_url" => "//localhost:3000/admin/carousel/show?page=1"
"from" => 1
"last_page" => 4
"last_page_url" => "//localhost:3000/admin/carousel/show?page=4"
"next_page_url" => "//localhost:3000/admin/carousel/show?page=2"
"path" => "//localhost:3000/admin/carousel/show"
"per_page" => 6
"prev_page_url" => null
"to" => 6
"total" => 20
这里的问题是,在视图中,幻灯片键不再可访问。抛出以下错误(仅当我使用
->paginate()
)。
E_ERROR Trying to get property 'slides' of non-object
我在文档和其他来源中读到的所有内容都表明这是一个非常基本的操作。就我个人而言,我不明白为什么它会导致这样的问题。我应该能够通过简单地嵌套foreach在视图中访问它。
在此方面的任何帮助都将不胜感激。