I'm building an Employee Management system and The employee part works good but when i try to create for family members for an employee i have a problem. I was hoping i can create a family member for an employee in such a way localhost/mysite/employees/5/families/create
Here are my files P.S - I have omitted some of the code which i thought was irrelevant. For example, there are more than 30 employee fields that i save. For this question i just displayed the 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 (Model)
<?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 (For now i only posted the index and create methods)
<?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 (Model)
<?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');
}
}
Result from 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 | |
+--------+----------+------------------------------------------------+----------------------------+--------------------------------------------------+------------+
When i navigate to http://localhost/mysite/public/employees/1/families/create i can see the create form but it's not getting the employee data. i did a vardump of $employee on that page and i was expecting to see the data for EmployeeID = 1 (as you can see from the url), but it was blank. Funny thing is that it's not throwing an error, it receives the $employee data passed from the controller but it a blank data.
So what could be the problem?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire