I want to paginate all departments with corresponding companies on my view. Therfore I created the corresponding models and to select only the needed fields i created a collection. In my Controller i create an Instance of this Collection and want to pass it to my View. Here's my Controller:
$deps = new App\Http\Resources\DepartmentCollection(
App\Department::where('active',1)->whereHas('company',function($query){
$query->where('active',1);
})->with('company')->paginate(10));
Here's my Collection:
public function toArray($request)
{
$arr = array();
foreach($this as $t)
{
$a = array();
$a['id'] = $t->id;
$a['title'] = $t->title;
$a['company_title'] = $t->company['title'];
$a['company_id'] = $t->company['id'];
$arr[] = $a;
}
return $arr;
}
When I return the $deps in my Controller, so I can see the raw data it looks fine.
data: {
0 : {
id : 1
title : "Prod.asdfasdf"
company_title : "asdf"
company_id : 1
But whenever I pass the $deps to my View, i always get the complete models, as if there was never any Collection.
return $deps;
return view('abteilungen',['departments'=>$deps]);
What am I doing wrong?
EDIT: I tried using
return view('abteilungen',['departments'=>$deps->toArray($request)]);
but that deleted the pagination information provided by Laravel, and also the access to
$departments->links()
in my View wouldn't work.