1

First take a look my working file.

ActionController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Action;
use App\Role;
use App\Http\Requests;

class ActionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //

        $actions = Action::where('id','>=',1)->paginate(10);

        $roles = Role::all();
        $data = ['roles' => $roles];
        return view('actions.index',['actions'=>$actions]);
    }

index.blade.php:

<table id="example2" class="table table-bordered table-hover" width="100%">

    <thead>
        <tr>
            <th>URI</th>
            <th>Action</th>
            @foreach($roles as $role)
               <th>{{$role->role}}</th>
            @endforeach
        </tr>
    </thead>
        <tbody>
    @foreach($actions as $action)

        <tr>
            <td>{{$action->uri}}</td>
            <td>{{$action->action}}</td>
            <td>{{$action->role}}</td>
        </tr>

    @endforeach
    </tbody>
</table>

When i trying to show actions in foreach its working fine but when i want to show roles in foreach loop then its showing Undefined variable: roles error. How can i show the roles in actions index?

6 Answers 6

2

Change in your ActionController.php. You need to create an array you want access in the view, so create an array called $data and pass it to the view. After passing array in the view you will be able to access variable you have passed in the controller like you have passed roles and actions so you can access it by it's name $roles and $actions from the view.

public function index(){
    $actions = Action::where('id','>=',1)->paginate(10);

    $roles = Role::all();
    $data = ['roles' => $roles, 'actions'=>$actions];
    return view('actions.index',$data); // 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Because you were not passing $roles variable within your view it should be passed it like as

return view('actions.index',['actions'=>$actions])->withRoles($roles);

or you can simply pass it like as of actions

return view('actions.index',['actions'=>$actions,'roles' => $roles]);

or

$data = ['actions'=>$actions,'roles' => $roles];
return view('actions.index',$data);

Comments

1

try this:

public function index()
{
    //

    $actions = Action::where('id','>=',1)->paginate(10);

    $roles = Role::all();
    return view('actions.index',array('actions'=> $actions, 'roles' => $roles));
}

Comments

1

You are not passing the data variable to your view, it should be added to the array you are passing.

return view(
    'actions.index',
    [
        'actions' => $actions,
        'roles' => $roles
    ]
);

Comments

1

Since you have same names both for variables and collections, it's better to use compact():

return view('actions.index', compact('roles', 'actions'));

This will automatically convert names to an array and will pass all the data into the view.

Comments

1

This looks more cleaner. php compact

return view('actions.index', compact('roles', 'actions'));

1 Comment

Thanks.Its helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.