I don't think you need get()->paginate(5) just paginate(5).
Jul 25, 2017
33
Level 2
Method does not exist
Hello Everyone,
I am trying to display some results in a view but I am getting the below error:
Method service does not exist.
In my controller I have the below:
$id = Auth::id();
$comp_id = User::find($id)->company->pluck('id');
$company = Company::findOrFail($comp_id);
$service = $company->service()->paginate(5);
return view('services.index')->with(['service' => $service]);
and in my view:
@foreach ($service as $serv)
<tr>
<td>{{ $serv->service_type }}</td>
<td>{{ $serv->service_type_name }}</td>
<td>{{ $serv->description }}</td>
<td></td>
</tr>
@endforeach
and I get the below error if I change it to the below:
@foreach ($service->company as $serv)
<tr>
<td>{{ $serv->service_type }}</td>
<td>{{ $serv->service_type_name }}</td>
<td>{{ $serv->description }}</td>
<td></td>
</tr>
@endforeach
and if I change my controller:
$id = Auth::id();
$comp_id = User::find($id)->company->pluck('id');
$company = Company::findOrFail($comp_id);
$service = Company::where('id', $comp_id)->get()->paginate(5);
return view('services.index')->with(['service' => $service]);
and the error:
Method paginate does not exist.
The Service model is the below:
public function company()
{
return $this->belongsTo(Company::class, 'company_service')->withPivot('company_id', 'service_id', 'site_id');
}
I have similar code working else where but I can't for the life of me see what I am doing wrong.
Thanks
Level 3
Yep again an error of mine, didn't try it on a real project.
foreach(Auth::user()->company as $company){
$services = $services->merge($company->service);
}
Please or to participate in this conversation.