未定义变量:雇员

时间:2019-05-08 04:21:54

标签: php laravel laravel-5

我想在用户页面上显示图片库

这是我来自ImageUserController的代码

function list(){
    $employees['img_user'] = ImageUser::where('user_id', Auth::user()->id)->get();

    return view('ukm.index', $employees);
}

这是我来自UserContoller

的代码
public function list()
{
    $employees = ImageUser::all();
    $ukm    = Ukm::all();

    return view('/ukm/index', compact( 'employees', 'ukm'));
}

这是我的路线

Route::get('/ukm/index', 'ImageUserController@list');

这是我在ukm/index.blade.php上的代码

table class="table table-stripped table-bordered">
        <thead class="thead-dark">
          <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Description</th>
            <th scope="col">Image</th>
            <th scope="col">Edit</th>
            <th scope="col">Delete</th>
          </tr>
        </thead>
        <tbody>
        @foreach ($employees as $employee)
          <tr>
            <th scope="row">{{ $employee->id }}</th>
            <td>{{ $employee->name }}</td>
            <td>{{ $employee->description }}</td>
            <td><img src="{{ asset('uploads/employee/' . $employee->image) }}" width="150px;" height="100px;" alt="Image"></td>
            <td><a href="/img_user/editimage/{{ $employee->id }}" class="btn btn-success">Edit</a></td>
            <td><a href="/img_user/deleteimage/{{ $employee->id }}" class="btn btn-danger">Delete</a></td>
          </tr>
        @endforeach
        </tbody>
      </table>

我遇到此错误:Undefined variable: employees

2 个答案:

答案 0 :(得分:1)

您实际上并没有将数组传递给视图

return view('ukm.index')->with('employees', $employees); 

答案 1 :(得分:1)

在您的ImageUserController list()方法中,

function list(){
    $employees = ImageUser::where('user_id', Auth::user()->id)->get();

    return view('ukm.index', compact('employees'));
}