簡體   English   中英

Laravel-函數的參數太少(插入數據)

[英]Laravel - Too few arguments to function (Insert data)

我真的很需要幫助,因為我無法弄清楚問題出在哪里,而且已經無法找到答案。

我需要從一個視圖中插入有關員工的數據,這應該非常簡單。 所以我有兩個表-“部門”和“雇員”。 當我打開“部門”視圖時,它將顯示每個部門,並且我可以進一步選擇一個部門,並查看在該部門工作的所有員工,我可以刪除員工。 至此一切正常。 但是,當我想添加員工時,它會顯示錯誤-參數太少(...)。

路線

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();
  }

員工模式

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(查看)

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>

錯誤行

 Symfony \ Component \ Debug \ Exception \
 FatalThrowableError (E_RECOVERABLE_ERROR)
 Too few arguments to function 
 App\Http\Controllers\EmployeeController::store(), 1 passed and exactly 5 expected

您的代碼中有多個問題,我將一一指出:

  1. 您沒有帶有操作的表單標簽
  2. 您正在使用1個參數(在javascript中)調用該函數,但需要5個參數($ id,$ name,$ surname,$ email,$ phone)
  3. 向數據庫插入內容的路由需要發布而不是獲取
  4. 將數據作為獲取參數傳遞給數據庫時,您必須要小心,因為它非常不安全

我將為您提供一個簡單的示例,以便您掌握本質

您的視圖(假設它稱為new-user.blade.php):

<form method="post" action="{{ route('saveData') }}">
<input type="text" name="name" />
<button type="submit">Save</button>
</form>

您的控制器(帶有saveData方法)-假設您的控制器名為UserController

加載添加新的用戶表單方法:

public function userForm()
{
return view('new-user');
}

將數據保存到數據庫:

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!');
}

您的路線(在web.php文件中)

Route::get('/new-user', 'UserController@userForm'); //加載表格

Route::post('/create-user', 'UserController@saveData')->name('saveData'); //將數據保存到數據庫

問題與方法參數有關。

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();
}

另外,輸入標簽沒有名稱屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM