外观\点火\异常\视图异常 尝试获取 index.blade 中非对象的属性“名称.php

我有两个桌子部门和员工。我想列出部门或员工。该部门还可以,但是当我想看到员工时,它会给我这个错误。我已经尝试了一些解决方案,但它没有帮助我,它仍然显示这个错误


这是员工\索引.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)

紫衣仙女
浏览 99回答 1
1回答

繁星淼淼

所以我会假设这种关系是一个部门有很多员工。这是设置模型关系的方式。<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Employee extends Model{&nbsp; &nbsp; public function department()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo(Department::class);&nbsp; &nbsp; }}<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Department extends Model{&nbsp; &nbsp; public function employees()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasMany(Employee::class);&nbsp; &nbsp; }}这是从数据库部分设置关系的方法(如果您没有正确拥有它)public function up()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Schema::create('departments', function (Blueprint $table) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->bigIncrements('id');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->string('name');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->timestamps();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }public function up()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Schema::create('employees', function (Blueprint $table) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->bigIncrements('id');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->string('name');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->bigInteger('department_id')->unsigned();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->foreign('department_id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->references('id')->on('departments')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->onDelete('cascade');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->timestamps();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP