Pankaj
Pankaj

Reputation: 10095

Method with does not exist

Below is my code to get all users and role info for each user.

$Users = \App\Models\User\User_Model::all()
        ->with("Role")->get();
return view("User.List", array("Users" => $Users));

In my user model, A function is defined like below.

public function Role()
{
    $this->hasOne("\App\Models\User\Role_Model", "RoleID");
}

I am getting below error

Method with does not exist.

Question ? Am I missing anything ?

Upvotes: 2

Views: 6608

Answers (1)

ceejayoz
ceejayoz

Reputation: 179994

You need to get rid of the all() call (which internally does a get() to turn your query builder into a collection of results).

$Users = \App\Models\User\User_Model::with("Role")->get();

See also @h2ooooooo's comment that your Role() relationship should return the relationship.

Upvotes: 4

Related Questions