Reputation: 3
I'm in really need of help as I can't figure out what's the problem and have browsed for answers unsuccessfully.
I need to insert data about an employee from a view which should be pretty simple. So i have two tables - ''Department'' and ''Employees''. When I open the view ''Department'' it shows each department and I can further choose one of the departments and see all of the employees that work in that department, I can delete the employees. Everything works to this point. But when I want to add an employee it displays the error - Too few arguments (...) .
Routes
Route::get('department/', 'DepartmentController@index');
Route::get('department/delete/{id}','DepartmentController@destroy');
Route::get('employee/new/{id}', 'EmployeeController@new');
Route::get('employee/insert/{id}', 'EmployeeController@store');
Route::get('employee/{id}', 'EmployeeController@index');
EmployeeController
namespace App\Http\Controllers;
use App\Employee;
use App\Department;
use Illuminate\Http\Request;
class EmployeeController extends Controller
{
public function index($department_id){
$employees=Employee::where('department_id','=',$department_id)->get();
return view('employees' ['department_id'=>$department_id,'employees'=>$employees]);
}
public function new ($department_id){
$departments=Department::where('department_id','=',$department_id)->get();
return view('employee_new',['department_id'=>$department_id,'department_name'=>$departments[0]->department_name]);
}
public function store( $id, $name, $surname, $email, $phone){
$employee = new Employee;
$employee->employee_name = $name;
$employee->employee_surname = $surname;
$employee->employee_email = $email;
$employee->employee_phone = $phone;
$employee->department_id = $id;
$employee->save();
}
Employee Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model{
protected $table = 'employees';
public function department() {
return $this->belongsTo('App\Department');
}
}
employee_new.blade (view)
Adding a new employee for <b>{{ $department_name }}</b> department:
<table>
<tr>
<td>Employee Name<td>
<td>:</td>
<td><input type="text" id="employee_name"></td>
<td>Employee Surname<td>
<td>:</td>
<td><input type="text" id="employee_surname"></td>
<td>Employee Email<td>
<td>:</td>
<td><input type="text" id="employee_email"></td>
<td>Employee Phone number<td>
<td>:</td>
<td><input type="text" id="employee_phone"></td>
<td><input type="button" value="add" onclick="addEmployee()"></td>
</tr>
</table>
<script>
function addEmployee() {
window.location.href="/employee/insert/"+{{$department_id}};
}
</script>
Error line
Symfony \ Component \ Debug \ Exception \
FatalThrowableError (E_RECOVERABLE_ERROR)
Too few arguments to function
App\Http\Controllers\EmployeeController::store(), 1 passed and exactly 5 expected
Upvotes: 0
Views: 2438
Reputation: 359
You have more than one issues on your code, I will point them one by one:
I will provide you with a simple example so you can grab the essence
Your view (lets suppose that is called new-user.blade.php):
<form method="post" action="{{ route('saveData') }}">
<input type="text" name="name" />
<button type="submit">Save</button>
</form>
Your controller (with saveData method) - let's suppose that your controller is called UserController
Load add new user form method:
public function userForm()
{
return view('new-user');
}
Save data to the database:
public function saveData(Request $request)
{
$user = new User;
$user->name = $request->name;
$user->save();
// or User::create($request->all());
return view('view-name')->with('status', 'User was saved!');
}
Your routes (on web.php file)
Route::get('/new-user', 'UserController@userForm');
// to load the form
Route::post('/create-user', 'UserController@saveData')->name('saveData');
// to save data to the dabase
Upvotes: 1
Reputation: 943
The problem is related to method arguments.
public function store(Request $request, $id){
$employee = new Employee;
$employee->employee_name = $request->employee_name;
$employee->employee_surname = $request->employee_surname;
$employee->employee_email = $request->employee_email;
$employee->employee_phone = $request->employee_phone;
$employee->department_id = $id;
$employee->save();
}
Also input tags do not have a name attribute.
Upvotes: 0