我有两个桌子部门和员工。我想列出部门或员工。该部门还可以,但是当我想看到员工时,它会给我这个错误。我已经尝试了一些解决方案,但它没有帮助我,它仍然显示这个错误
这是员工\索引.php
----
<table class="table">
<tr>
<td>Name</td>
<td>Department</td>
</tr>
@foreach($employees as $employee)
<tr>
<td>{{ $employee->name }}</td>
<td>{{ $employee->department->name }}</td>
</tr>
@endforeach
</table>
---
这是网络.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
//Route::get('login/github', 'Auth\LoginController@redirectToGithub');
//Route::get('login/github/callback', 'Auth\LoginController@handleGithubCallback');
Route::resource('departments', 'DepartmentsController');
Route::resource('employees', 'EmployeesController');
Route::get('/home', 'HomeController@index')->name('home');
部门/索引刀片.php
...
<table class="table">
<tr>
<td>Name</td>
</tr>
@foreach($departments as $department)
<tr>
<td>{{ $department->name }}</td>
</tr>
@endforeach
</table>
...
员工控制器
public function index()
{
$employees = \App\Employee::orderBy('id', 'desc')->paginate(10);
return view('employees.index')->with('employees', $employees);
}
部门控制器
public function index()
{
$departments = \App\Department::all();
$departments = \App\Department::orderBy('id', 'desc')->get();
$departments = \App\Department::orderBy('id', 'desc')->paginate(5);
return view('departments.index')->with('departments', $departments);
}
im 得到的错误是外观\点火\异常\视图异常 尝试获取非对象的属性“名称”(视图:C:\xampp\htdocs\Laravel\test2\资源\视图\员工\索引.blade.php)
繁星淼淼