Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mattnewark's avatar

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

0 likes
33 replies
topvillas's avatar

I don't think you need get()->paginate(5) just paginate(5).

mattnewark's avatar

Hi @topvillas,

If I remove get() I get the below error:

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$company

If I remove paginate() I get this error:

Undefined property: Illuminate\Database\Eloquent\Builder::$company

Thanks

topvillas's avatar

Is companies/services a many to many relationship?

mattnewark's avatar

Hi, My Company model:

public function service()
  {
    return $this->belongsToMany(Service::class, 'company_service')->withPivot('company_id', 'service_id', 'site_id');
  }

My Service model:

public function company()
    {
      return $this->belongsToMany(Company::class, 'company_service')->withPivot('company_id', 'service_id', 'site_id');
    }

Thanks

Parasoul's avatar

Hey, since it's many to many better have your relation at plurial (more clear it's a many to many) so service => services and company => companies.

There is no error in your controller as far i search , however you can change it for this :

$services = Auth::user()->company->service()->paginate(5);
return view('services.index', compact('services'));

And then in your view accessing it like you did.

@foreach ($services as $service)
    <tr>
        <td>{{ $service->service_type }}</td>
        <td>{{ $service->service_type_name }}</td>
        <td>{{ $service->description }}</td>
    // If you want to acces the company of the service
    <td> {{$service->company->name}}</td>
    </tr>
@endforeach 
mattnewark's avatar

Hi @Parasoul,

I have made the above changes but I am still getting the below:

Method service does not exist.

Thanks

Parasoul's avatar

Can you send your user/company/service models and your version of laravel.

mattnewark's avatar

Hi @Parasoul,

My User model:

public function company()
    {
      return $this->belongsToMany(Company::class)->withTimestamps();
    }

    public function site()
    {
      return $this->belongsToMany(Site::class)->withPivot('site_id','user_id','admin_id')->withTimestamps();
    }

My Company Model:

public function user()
  {
    return $this->belongsToMany(User::class)>withTimestamps();
  }

  public function service()
  {
    return $this->belongsToMany(Service::class, 'company_service')->withPivot('company_id', 'service_id', 'site_id');
  }

  public function site()
  {
    return $this->hasMany(Site::class);
  }

and my service Model:

public function company()
    {
      return $this->belongsToMany(Company::class, 'company_service')->withPivot('company_id', 'service_id', 'site_id');
    }

    public function site()
    {
      return $this->belongsToMany(Site::class, 'company_service')->withPivot('company_id', 'service_id', 'site_id');
    }

Thanks

mattnewark's avatar

This is the first few lines of the error:

(1/1) BadMethodCallException
Method service does not exist.
in Macroable.php (line 74)
at Collection->__call('service', array())
in ServiceController.php (line 30)
at Collection->service()
in ServiceController.php (line 30)
at ServiceController->index()
at call_user_func_array(array(object(ServiceController), 'index'), array())
in Controller.php (line 55)
at Controller->callAction('index', array())
in ControllerDispatcher.php (line 44)
at ControllerDispatcher->dispatch(object(Route), object(ServiceController), 'index')
in Route.php (line 204)
at Route->runController()
in Route.php (line 160)
at Route->run()
in Router.php (line 574)

In the service controller line 30 is:

$services = Auth::user()->company->service()->paginate(5);

Thanks

Parasoul's avatar

Ok, an user can have many company? Again got lure by the fact you call your relation company instead of companies. It's a many to many relation. So it returns a collection of company and not one company.

Do you want to return the services from all companies or just from one?

mattnewark's avatar

Hi @Parasoul,

It will bring back all the services for one company:

$id = Auth::id();
      $comp_id = User::find($id)->company->pluck('id');
      $company = Company::findOrFail($comp_id);
      $services = Auth::user()->company->service()->paginate(5);
      return view('services.index', compact('services'));

So I have the user ID that is related to a company ID and then I can get all the services for that company..

Thanks

Parasoul's avatar

In your controller, if you do dd($comp_id); you'll get a array of all company ids related to a user.

Then you do a findOrFail not a firstOrFail so it gather all companies of the user in a collection.

So you want the services of all companies associated to an user or just one of the companies?

mattnewark's avatar

For this instance I would like all the services of the companies that are associated to a user. So for this instance the user that is logged in has only one company associated to them but there can be situations were a user can have multiple companies.

Thanks

Parasoul's avatar

You can do smth like this in your controller :

use Illuminate\Pagination\LengthAwarePaginator;

//Creating an empty collection for our services
$services = collect([]);

//Fetching all services from all companies of our user
foreach(Auth::user()->company as $company){
    $services = $services->merge($company->service);
}

//Now we will have to create the paginator manually since we already have a //collection and not a query/relation
//@see https://laravel.com/docs/5.4/pagination#manually-creating-a-paginator

//For this we need the number of services
$nbServices = $services->count();

//Our current page in the pagination
$currentPage = request()->get('page') ? request()->get('page') : 1;

//The number of services per page we want
$nbServicesPerPage = 5;

//Fetching the services for our current page
$servicesForCurrentPage = $services->slice(($currentPage - 1) * $nbServicesPerPage, $nbServicesPerPage);

//Creating the paginator instance 
$servicesPaginator = new LengthAwarePaginator($services, $nbServices, $nbServicesPerPage);

return return view('services.index', ['services' => $servicesPaginator]);
mattnewark's avatar

Hi @Parasoul,

Would I replace everything in my index function or just the $services part?

$id = Auth::id();
      $comp_id = User::find($id)->company->pluck('id');
      $company = Company::findOrFail($comp_id);
      $services = Auth::user()->company->service()->paginate(5);
      return view('services.index', compact('services'));

Thanks

Parasoul's avatar

Everything !

$id = Auth::id();
      $comp_id = User::find($id)->company->pluck('id');
      $company = Company::findOrFail($comp_id);
      $services = Auth::user()->company->service()->paginate(5);
      return view('services.index', compact('services'));

I'll add some comment to help you understand.

mattnewark's avatar

Hi @Parasoul,

On the positive side I do not get any errors but on the negative I have no results either.

Parasoul's avatar

Idd i did a mistake replace this line :

$servicesForCurrentPage = $services->slice($nbServicesPerPage, ($currentPage - 1) * $nbServicesPerPage);

by this line :

$servicesForCurrentPage = $services->slice(($currentPage - 1) * $nbServicesPerPage, $nbServicesPerPage);
Parasoul's avatar

Again an error form me :

foreach(Auth::user()->company as $company){
    $services->merge($company->service());
}

by

foreach(Auth::user()->company as $company){
    $services->merge($company->service);
}
mattnewark's avatar
Collection {#369 ▼
  #items: []
}

Looks like it is not getting anything through..

Parasoul's avatar

In the foreach loop, dd($company->service)

mattnewark's avatar

Morning @Parasoul,

I do get the below:

Collection {#385 ▼
  #items: array:19072 [▼
    0 => Service {#19472 …25}
    1 => Service {#19473 …25}
    2 => Service {#19474 …25}
    3 => Service {#19475 …25}
    4 => Service {#19476 …25}
    5 => Service {#19477 …25}
    6 => Service {#19478 …25}
    7 => Service {#19479 …25}
    8 => Service {#19480 …25}
    9 => Service {#19481 …25}
    10 => Service {#19482 …25}

There are more this is just a snippet.

Thanks

Parasoul's avatar
Parasoul
Best Answer
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);
}
mattnewark's avatar

I get this error:

(1/1) FatalErrorException
Allowed memory size of 134217728 bytes exhausted (tried to allocate 3895990 bytes)
in ManagesLayouts.php (line 152)

Thanks

Parasoul's avatar

You do have 19000 service in one company?

Next

Please or to participate in this conversation.