Undefined error Property

Asked

Viewed 1,135 times

1

I’m getting the following error by clicking on permissions, button that directs to this controller:

Error:

Undefined Property: App Http Controllers Rolescontroller::$role

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Role;

class RolesController extends Controller
{
//Exibe Lista de Roles
public function ListRoles(role $role)
{
    $roles = $role->all();

    return view('Roles/roles', compact('roles'));
}


//Exibe Formulário de edição
public function edit($idRole)
{
    $role = role::find($idRole);

    if(empty($role)){
        return view('alert');
    }

    return view('Roles/roles-update', compact('role'));
}


//Salva alterações do formulário de edição
public function save($idRole)
{
    $name = Request()->input('name');
    $label = Request()->input('label');

    $role = role::find($idRole);
    $role->name = $name;
    $role->label = $label;
    $role->save();

    return redirect()->action('RolesController@ListRoles')->withInput();
}


//Deletar uma Role
public function delete($idRole)
{
    $role = role::find($idRole);
    $role->delete();

    return redirect()->action('RolesController@ListRoles');
}


//enviar permissao da role para a view de permissao da role
public function permissions($id)
{
    $role = $this->role->find($id);

    $permissions = $role->permissions;

    return view('roles/permissions', compact('role', 'permissions'));
}
}

RESOLVED:

    public function permissions($idRole)
    {
       $role = role::find($idRole);

       $permissions = $role->permissions;

       return view('roles/permissions', compact('role', 'permissions'));
    }
  • The error is basically telling you that the method/property role does not exist. Check if this is present in the controller.

  • Role method is present

  • I can publish the entire controller if it helps

  • Help yes. Put the whole code to facilitate understanding.

  • I just didn’t think right, but you can understand

  • the error is specified that occurs on line 61, ie where I had shown

Show 1 more comment

1 answer

1


The estate roles for $this does not really exist. At the beginning of your class, declare:

private $role;

That it will exist, thus avoiding the current error on line 61, but when you call $this->roles this will probably return null, then you must create a Construct or Predispatch, depends on your Framework to define what your roles will be.

  • returned null even. I am beginner so I do not understand what would be a contruct/ Predispatch. You have how to explain me, pf?

  • Which framework you use?

  • I started using Laravel

  • I don’t use Laravel, but I think it’s something like public function __invoke($id) { /* aqui vai o código */ } This is a function that is called before any function or action of your controller, so in it you arrow your roles, giving pro parameter $this->roles the value of where they should come from initially, like a comic book for example.

  • I will try to understand how it works. VLW by help.

  • Hello, just to your knowledge, I managed to solve my problem, I will edit my question by putting the following answer.

  • Oops. Good! Put the answer yes, so that other people can also be helped in the future!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.