Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Flex's avatar
Level 4

Unable to Insert Data from Angular frontend to Laravel API whitch Model Relationship One to Many

I have Angular App connected with Laravel API. In Laravel API there are one to many relationship between Employee and Salary Models. when I try to insert data Only insert Employee Model data but not insert salary model data Employee Insert Controller is

public function addEmployee(Request $request) {
        $employee = Employee::create($request->all());
        $salary = $employee->salaries()->create($request->all());
        return response($employee, 201);
}

api route for insert

/add new Employee
Route::post('addEmployee','App\Http\Controllers\EmployeeController@addEmployee');

employee-add.html

<div class="detail-list">
    <h3>Employee Profile</h3>
    </div>
    <form (ngSubmit)="insertData()">
        <h4>Profile Detail</h4>
        
     <div class="form-group row">
        <label for="first_name" class="col-sm-2 col-form-label">First Name</label>
        <div class="col-sm-10">
        <input type="text" name="first_name" class="form-control" [(ngModel)]="employee.first_name">
        </div>
     </div>
     <br>
     <div class="form-group row">
        <label for="last_name" class="col-sm-2 col-form-label">Last Name</label>
        <div class="col-sm-10">
        <input type="text" name="last_name" class="form-control" [(ngModel)]="employee.last_name">
        </div>
     </div>
     <br>
     <div class="form-group row">
        <label for="birth_date" class="col-sm-2 col-form-label">Date of Birth</label>
        <div class="col-sm-10">
        <input type="date" name="birth_date" class="form-control" [(ngModel)]="employee.birth_date">
        </div>
     </div>
     <br>
     <div class="form-group row">
        <label for="hire_date" class="col-sm-2 col-form-label">Hire Date</label>
        <div class="col-sm-10">
        <input type="date" name="hire_date" class="form-control" [(ngModel)]="employee.hire_date">
        </div>
     </div>
     <br>
     <div class="form-group row">
        <label for="gender" class="col-sm-2 col-form-label">Gender</label>
        <div class="col-sm-10">
        <input type="text" name="gender" class="form-control" [(ngModel)]="employee.gender">
        </div>
     </div>
     <br>
     
     <h4>Salary Details</h4>
     
     <div class="salary-details"  >
       <div class="table">
          <div class="form-group row">
             <label for="salary" class="col-sm-2 col-form-label">Salary</label>
             <div class="col-sm-10">
             <input type="text" name="salary" class="form-control" [(ngModel)]="salary.salary">
             </div>
          </div>
          <br>
          <div class="form-group row">
             <label for="from_date" class="col-sm-2 col-form-label">From Date</label>
             <div class="col-sm-10">
             <input type="date" name="from_date" class="form-control" [(ngModel)]="salary.from_date">
             </div>
          </div>
          <br>
          <div class="form-group row">
             <label for="to_date" class="col-sm-2 col-form-label">To Date</label>
             <div class="col-sm-10">
             <input type="date" name="to_date" class="form-control" [(ngModel)]="salary.to_date">
             </div>
          </div>
          <br>
       </div>
     </div>
    
      
    <br>
    <button  class="btn btn-dark btn-sm mt-4 btnPosition">Add Employee</button>
    <br><br>
    </form>
    

angular data.service.ts for insert data

insertData(data: Employee) {
    return this.httpClient.post('http://127.0.0.1:8000/api/addEmployee', data);
  }

and employee-add.component.ts

export class EmployeeAddComponent implements OnInit {
  
  employees:any;
  employee = new Employee();
  salary: any;
  title: any;
  

  constructor(private dataService:DataService) {}
  ngOnInit(): void {
    
  }

  

  insertData() {
    this.dataService.insertData(this.employee).subscribe(res => {
      console.log(res);
      
    })
    
  }

}

Employee model like

export class Employee {
    birth_date:any;
    first_name:any;
    last_name:any;
    gender:any;
    hire_date:any;
salaries: any;
salary:any;
emp_no: any;
}

but console encontered following error msg about salary ERROR TypeError: ctx.salary is undefined I think I need relationship between Employee Model and Salary Model in Angular? how could I fix this matter

0 likes
10 replies
Tray2's avatar

This is insanely dangerous,

  $employee = Employee::create($request->all());
  $salary = $employee->salaries()->create($request->all());

You need to validate the data before trying to create any records, and you should never pass $request->all() to your database.

The validation will most likely fail in your case, or maybe it's just trying to insert things into your database into columns that does not exist, but who knows since you don't share any error messages .

Flex's avatar
Level 4

@Tray2 got following error msgs on the console ERROR Object { headers: {…}, status: 500, statusText: "Internal Server Error", url: "http://127.0.0.1:8000/api/addEmployee", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://127.0.0.1:8000/api/addEmployee: 500 Internal Server Error", error: {…} } core.mjs:11458:22 The connection to ws://localhost:4200/ng-cli-ws was interrupted while the page was loading

error { target: WebSocket, isTrusted: true, srcElement: WebSocket, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, returnValue: true, defaultPrevented: false, composed: false, … }

Tray2's avatar

@Flex That tell me that your insert fails due to faulty data, or at least unexpected data. You need to dive into the logs and see if there is a more descriptive error message there

Flex's avatar
Level 4

@Tray2 I think problem with Angular form. its salary inputs unable to identify Employee Model relationship as well <input type="text" name="salary" class="form-control" [(ngModel)]="salary.salary"> how could I input Employee Model relationship with salary inputs?

Flex's avatar
Level 4

@Tray2 when I put my salary input this error not comming <div class="salary-details" *ngFor="let salary of employee.salaries">

<h4>Salary Details</h4>
     
     <div class="salary-details"  *ngFor="let salary of employee.salaries">
       <div class="table">
          <div class="form-group row">
             <label for="salary" class="col-sm-2 col-form-label">Salary</label>
             <div class="col-sm-10">
             <input type="text" name="salary" class="form-control" [(ngModel)]="salary.salary">
             </div>
          </div>
          <br>
          <div class="form-group row">
             <label for="from_date" class="col-sm-2 col-form-label">From Date</label>
             <div class="col-sm-10">
             <input type="date" name="from_date" class="form-control" [(ngModel)]="salary.from_date">
             </div>
          </div>
          <br>
          <div class="form-group row">
             <label for="to_date" class="col-sm-2 col-form-label">To Date</label>
             <div class="col-sm-10">
             <input type="date" name="to_date" class="form-control" [(ngModel)]="salary.to_date">
             </div>
          </div>
          <br>
       </div>
     </div>
    

but datainsert is not loop how could I manage this with proper Angular directive?

Tray2's avatar

@Flex Read what I have written in my previous replies. 500 is a server error, so most likely you get an error when trying to insert data. Most likely that a column receives a null value, when it's not nullable. Check your developer tools on the failing request, there is likely more information available there, and check the laravel logs.

Flex's avatar
Level 4

@Tray2 console indicate following errror msg The GET method is not supported for route api/addEmployee. Supported methods: POST. but I am using here POST method

Tray2's avatar

@Flex No, if the tools say you use get, well then you use get.

Flex's avatar
Level 4

@Tray2 yes I fount in console print error SQLSTATE[HY000]: General error: 1364 Field 'SALARY' doesn't have a default value (SQL: insert into SALARIES (emp_no) values (12 it means salary input fiels in Angular side does not insert data to Laravel API. now I need to create a relationship in the salary inputs <input type="text" name="salary" class="form-control" [(ngModel)]="salary.salary"> coming from Employee Model

Please or to participate in this conversation.