Gagan
Gagan

Reputation: 5656

Can't call a function defined in a model from a blade view

I have a User model. The User Model is related to the Role model such that each and every user can have only 1 role assigned to a user. One of the role is a Vendor role. In order to check that the current user is a vendor I do something like this

    protected function isVendor()
{
     $roles = Role::where('role_slug', 'vendor')->get();
     if(isset($roles) && $roles->count() > 0)
        return true;
    return false;

}

and I use the following code from my blade to state that the current user is a vendor and perform some more actions (like displaying some more panels)

<p>{!! Auth::user()->isVendor !!}</p>

However I keep getting this error

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

Would you know why this is happening ?

Thanks

Upvotes: 0

Views: 549

Answers (1)

Sh1d0w
Sh1d0w

Reputation: 9520

First of all, your method is protected, which means you can not call it like this. Change it to public.

Second, you are trying to access the function like propery, so Eloquent is trying to resolve a relationship, instead if calling your function. So the correct way to call your function is:

<p>{!! Auth::user()->isVendor() !!}</p>

Upvotes: 1

Related Questions