嵌套视图无法从Laravel 5.1中的父控制器获取数据

时间:2015-10-29 23:24:29

标签: laravel laravel-5.1

我正在构建一个员工管理系统,员工部分工作正常,但当我尝试为员工创建家庭成员时,我遇到了问题。 我希望我能以这种方式为员工创建一个家庭成员 本地主机/ mysite的/雇员/ 5 /家庭/创建

这是我的档案 P.S - 我省略了一些我认为无关紧要的代码。例如,我保存了超过30个员工字段。对于这个问题,我只显示了FirstName

routes.php文件

Route::resource('employees', 'EmployeesController');
Route::resource('employees.families', 'FamiliesController');

EmployeesController.php

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Employee;
use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;


class EmployeesController extends Controller
{

    public function index()
    {              
        //Stuff that works well on employees/index
    }

    public function create()
    {
        //Stuff that works well on employees/create
    }

    public function store(Request $request)
    {
        //Stuff that works well     
    }


    public function show(Employee $employee)
    {
        $employee=Employee::find($EmployeeID);
        return view('employees.show', compact('employee'));
    }


    public function edit($EmployeeID)
    {
        $employee=Employee::find($EmployeeID);          
        return view('employees.edit',compact('employee'));
    }


    public function update(Request $request, $EmployeeID)
    {
         //Stuff that works well on employees/edit
    }

}

Employee.php(模型)

<?php

namespace App;

use Illuminate\Database\Eloquent\SoftDeletes;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $primaryKey = 'EmployeeID';

    protected $fillable=[
        'Name'
    ];

    public function families()
    {
        return $this->hasMany('App\Family');
    }
}

FamilyController.php(目前我只发布索引并创建方法)

<?php

namespace App\Http\Controllers;

use App\Employee;
use App\Family;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FamiliesController extends Controller
{
    public function index(Employee $employee)
    {
        return view('families.index', compact('employee'));
    }

    public function create(Employee $employee)
    {
        return view('families.create', compact('employee'));
    }

}

Family.php(模特)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Family extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

      protected $primaryKey = 'FamilyID';

    public function employees()
    {
        return $this->belongsTo('App\Employee');
    }
}

来自php artisan route的结果:list

+--------+----------+------------------------------------------------+----------------------------+--------------------------------------------------+------------+
| Domain | Method   | URI                                            | Name                       | Action                                           | Middleware |
+--------+----------+------------------------------------------------+----------------------------+--------------------------------------------------+------------+
|        | GET|HEAD | employees                                      | employees.index            | App\Http\Controllers\EmployeesController@index   |            |
|        | POST     | employees                                      | employees.store            | App\Http\Controllers\EmployeesController@store   |            |
|        | GET|HEAD | employees/create                               | employees.create           | App\Http\Controllers\EmployeesController@create  |            |
|        | PATCH    | employees/{employees}                          |                            | App\Http\Controllers\EmployeesController@update  |            |
|        | PUT      | employees/{employees}                          | employees.update           | App\Http\Controllers\EmployeesController@update  |            |
|        | DELETE   | employees/{employees}                          | employees.destroy          | App\Http\Controllers\EmployeesController@destroy |            |
|        | GET|HEAD | employees/{employees}                          | employees.show             | App\Http\Controllers\EmployeesController@show    |            |
|        | GET|HEAD | employees/{employees}/edit                     | employees.edit             | App\Http\Controllers\EmployeesController@edit    |            |
|        | GET|HEAD | employees/{employees}/families                 | employees.families.index   | App\Http\Controllers\FamiliesController@index    |            |
|        | POST     | employees/{employees}/families                 | employees.families.store   | App\Http\Controllers\FamiliesController@store    |            |
|        | GET|HEAD | employees/{employees}/families/create          | employees.families.create  | App\Http\Controllers\FamiliesController@create   |            |
|        | PUT      | employees/{employees}/families/{families}      | employees.families.update  | App\Http\Controllers\FamiliesController@update   |            |
|        | DELETE   | employees/{employees}/families/{families}      | employees.families.destroy | App\Http\Controllers\FamiliesController@destroy  |            |
|        | GET|HEAD | employees/{employees}/families/{families}      | employees.families.show    | App\Http\Controllers\FamiliesController@show     |            |
|        | PATCH    | employees/{employees}/families/{families}      |                            | App\Http\Controllers\FamiliesController@update   |            |
|        | GET|HEAD | employees/{employees}/families/{families}/edit | employees.families.edit    | App\Http\Controllers\FamiliesController@edit     |            |
|        | GET|HEAD | employees/{id}/delete                          |                            | App\Http\Controllers\EmployeesController@delete  |            |
+--------+----------+------------------------------------------------+----------------------------+--------------------------------------------------+------------+

当我导航到http://localhost/mysite/public/employees/1/families/create时,我可以看到创建表单,但它没有获得员工数据。我在该页面上做了一个$ employee的vardump,我希望看到EmployeeID = 1的数据(你可以从网址上看到),但它是空白的。有趣的是,它不会抛出错误,它接收从控制器传递的$ employee数据,但它是一个空白数据。

那可能是什么问题?

1 个答案:

答案 0 :(得分:0)

familiescontroller.php 上的create方法未获取我们正在使用的员工。以下是创建方法应该如何在

 public function create($EmployeeID)
    {
        $employee = Employee::findOrFail($EmployeeID);
        return view('families.create')->with('employee', $employee);
    }