Facade\Ignition\Exceptions\ViewException试图获取属性';名称';index.blade.php中非对象的
我有两个表:部门和员工。我想列出部门或员工。部门可以,但当我想看到员工时,它会给我这个错误。我尝试了一些解决方案,但它没有帮助我,它仍然显示这个错误。 这是employees\index.blade.phpFacade\Ignition\Exceptions\ViewException试图获取属性';名称';index.blade.php中非对象的,php,sql,laravel,Php,Sql,Laravel,我有两个表:部门和员工。我想列出部门或员工。部门可以,但当我想看到员工时,它会给我这个错误。我尝试了一些解决方案,但它没有帮助我,它仍然显示这个错误。 这是employees\index.blade.php ---- <table class="table"> <tr> <td>Name</td>
----
<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>
---
departments/index.blade.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);
}
我得到的错误是
外观\点火\异常\视图异常
试图获取非对象的属性“name”(视图:C:\xampp\htdocs\Laravel\test2\resources\views\employees\index.blade.php)
如果我返回\App\Employee::orderBy('id','desc')->paginate(10);我明白了
{"current_page":1,"data":[{"id":2,"name":"Laura","department":""},{"id":1,"name":"Ana","department":""}],"first_page_url":"http:\/\/localhost\/Laravel\/test2\/public\/employees?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost\/Laravel\/test2\/public\/employees?page=1","next_page_url":null,"path":"http:\/\/localhost\/Laravel\/test2\/public\/employees","per_page":10,"prev_page_url":null,"to":2,"total":2}
因此,我将假设这种关系是一个部门有许多员工。这就是设置模型关系的方式
所以我假设这个关系是一个部门有很多员工。这就是设置模型关系的方式
u可以使用{{$employee->department->name???}来消除此错误。u可以使用{{$employee->department->name???}来消除此错误。illumb\Database\elounce\Collection{307▼ #项目:阵列:2[▼ 0=>App\Employee{308▶} 1=>App\Employee{#309▶} ] }照亮\数据库\雄辩\收藏{#307▼ #项目:阵列:2[▼ 0=>App\Employee{308▶} 1=>App\Employee{#309▶} ] }酷,这看起来不错,你能做\App\Employee::with('department')->orderBy('id','desc')->paginate(10);
它给我这个错误:lluminate\Database\elounce\RelationNotFoundException调用模型[App\Employee]上未定义的关系[department]。好的,这意味着部门
和员工
之间没有关系,您在这些表之间设置了外键吗?非常感谢,我正试图查看它,这就是问题所在。问题解决了。我没有添加关系,这就是原因。酷,这看起来不错,您能用('department')执行\App\employee::吗->orderBy('id','desc')->paginate(10);
它给我这个错误:lluminate\Database\elounce\relationnotfound调用模型[App\Employee]上未定义的关系[department]。好的,这意味着部门
和员工
之间没有关系,您是否在这些表之间设置了外键?非常感谢,我正在尝试查看它,这就是问题所在。问题已经解决。我没有添加关系这就是原因。
{"current_page":1,"data":[{"id":2,"name":"Laura","department":""},{"id":1,"name":"Ana","department":""}],"first_page_url":"http:\/\/localhost\/Laravel\/test2\/public\/employees?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost\/Laravel\/test2\/public\/employees?page=1","next_page_url":null,"path":"http:\/\/localhost\/Laravel\/test2\/public\/employees","per_page":10,"prev_page_url":null,"to":2,"total":2}