In my User model I have a rolesList method, this returns an array of the roles assigned to the user. This method is invoked to check if a user is authorized to view a certain page or perform an action.
I also extended this User model with laracasts Presenter (for example to display the users fullname composed of the first and last name properties of the user model).
Now I also like to use the presenter to display all the roles assigned to a user as a string, but with the code below I get the following error:Call to undefined method App\Presenters\UserPresenter::rolesArray() so I get that the methods in the User model are not accessible the way I imagined, how do I do it correctly?
<?php namespace App;
use App\Presenters\UserPresenter;
use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;
class User extends Model {
use PresentableTrait;
/**
* The user presenter.
*
* @var class
*/
protected $presenter = UserPresenter::class;
/**
* User Roles
*
* @return object
*/
public function roles()
{
return $this->hasMany(UserRole::class);
}
/**
* Get the user roles in an array.
*
* @return \Illuminate\Http\Response
*/
public function rolesArray()
{
return array_map(function($item)
{
return $item['role'];
},
$this->roles->toArray());
}
}
<?php namespace App\Presenters;
use Laracasts\Presenter\Presenter;
class UserPresenter extends Presenter {
public function roles()
{
return implode(', ', $this->rolesArray());
}
}
I removed the unrelated code in the classes above.