【问题标题】:Why show Undefined variable error in Laravel 5.2?为什么在 Laravel 5.2 中显示未定义变量错误?
【发布时间】:2017-10-24 16:03:28
【问题描述】:

先看看我的工作文件。

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>

当我试图在foreach 中显示actions 时,它工作正常,但是当我想在foreach 循环中显示roles 时,它显示Undefined variable: roles 错误。如何在操作索引中显示角色?

【问题讨论】:

    标签: php mysql laravel laravel-5.2


    【解决方案1】:

    更改您的ActionController.php。您需要在视图中创建一个要访问的数组,因此创建一个名为$data 的数组并将其传递给视图。在视图中传递数组后,您将能够访问已在控制器中传递的变量,就像您传递了 rolesactions 一样,因此您可以从视图中通过其名称 $roles$actions 访问它。

    public function index(){
        $actions = Action::where('id','>=',1)->paginate(10);
    
        $roles = Role::all();
        $data = ['roles' => $roles, 'actions'=>$actions];
        return view('actions.index',$data); // 
    }
    

    【讨论】:

      【解决方案2】:

      因为您没有在视图中传递 $roles 变量,所以应该像传递它一样传递它

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

      或者你可以像actions一样简单地传递它

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

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

      【讨论】:

      • 哦,我的错。谢谢+1 :)
      【解决方案3】:

      试试这个:

      public function index()
      {
          //
      
          $actions = Action::where('id','>=',1)->paginate(10);
      
          $roles = Role::all();
          return view('actions.index',array('actions'=> $actions, 'roles' => $roles));
      }
      

      【讨论】:

        【解决方案4】:

        你没有将数据变量传递给你的视图,它应该被添加到你传递的数组中。

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

        【讨论】:

          【解决方案5】:

          由于变量和集合的名称相同,因此最好使用compact()

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

          这将自动将名称转换为数组并将所有数据传递到视图中。

          【讨论】:

            【解决方案6】:

            这看起来更干净。 php compact

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

            【讨论】:

            • 谢谢。它的帮助。
            猜你喜欢
            • 1970-01-01
            • 2016-04-01
            • 2016-06-02
            • 2016-08-17
            • 1970-01-01
            • 1970-01-01
            • 2016-04-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多