从Angular的Angular SPA到Laravel后端解析更新的数据时遇到问题

时间:2019-07-04 14:38:23

标签: php angular laravel crud edit

Am正在使用在前端使用Angular 8并在后端使用Laravel构建的单页应用程序。在执行一些CRUD操作时,在编辑部分,我有一个表单,其值是后端用户的值。当用户更新值时,正在捕获其数据并通过JWT发送到后端,效果很好。

问题是,当我使用 put方法(在后端路由上从前端传递更新的数据)时,出现错误。当我使用 post方法时,数据会正确通过。

从逻辑上讲这是错误的,因为更新数据时将put方法与特定资源的ID一起使用。我需要帮助从Angular前端传递带有URL的id,以便可以使用以下路由更新Laravel后端上的数据: Route :: put('editedData / {id}','SubmitFormController @ updateData' );

请协助?

Edit.component.html包含具有要更新值的表单

<form #editForm="ngForm" (ngSubmit)="onSubmit(editForm)">
    <!--Errors-->
    <div class="alert alert-danger" [hidden]="!error">
        {{ error }}
    </div>
<!--Children details-->
<div class="card-header childheader">Update Details</div>
    <div class="card-body">
         <div class="form-group row">
         <div class="col-sm-12">
            <label for="childFirstName">Child Name</label>
            <input 
                type="text" 
                name="childName" 
                class="form-control" 
                [ngModel]="singleUser?.name"
                id="childName" 
                placeholder="Child FirstName">
        </div>
    </div>
    <div class="form-group row">
         <div class="col-sm-6">
             <label for="childAge">Child Age</label>
            <select 
                class="form-control" 
                id="chAge" 
                name="childAge"
                [ngModel]="singleUser?.age" 
                required>
                <option value="" selected disabled> Child's Age</option>
                <option value="1"> 1 </option>
                <option value="2"> 2 </option>
                <option value="3"> 3 </option>
                <option value="4"> 4 </option>
            </select>
        </div>
        <div class="col-sm-6">
            <label for="childGender">Child Gender</label>
            <select 
                class="form-control" 
                id="childGender" 
                name="childGender" 
                [ngModel]="singleUser?.gender" 
                required>
            <option value="" style="display:none"> Child's Gender</option>
                <option value="Male"> Male</option>
                <option value="Female"> Female </option>
            </select>
        </div>
    </div>
       <!--END children-->
    <div class="form-group row">
        <div class="col-sm-12">
            <button 
                type="submit" 
                class="btn btn-lg btn-success btn-block" 
                [disabled]="!editForm.valid">Update Details </button>
        </div>
    </div>
    </div>
</form>

edit.component.ts捕获表单数据

import { Component, OnInit } from '@angular/core';
import { SharedService } from 'src/app/Services/shared.service';
import { AuthService } from 'src/app/Services/auth.service';

@Component({
  selector: 'app-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.component.css']
})
export class EditComponent implements OnInit {

  public singleUser : any[];
  public error = null;

  constructor(
    private Shared : SharedService,
    private Auth:AuthService,
  ) { }

  onSubmit(formValue) {
    const capture = {
      UserName: formValue.value.childName,
      Password: formValue.value.childAge,
      Gender: formValue.value.childGender,
    };

    return this.Auth
      .editedData(capture)
      .subscribe(
        data => console.log(data),
        error => console.log(error),
      );
  }

  ngOnInit() {
     this.Shared.edit$.subscribe(message => this.singleUser = message);
  }

}

验证服务以将更新的数据提交到后端

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class AuthService {

  private baseUrl = 'http://localhost/Laravel-anngular-spa/backend/public/api';

  constructor(private http:HttpClient) {}

  editedData(data:any){
    return this.http.post(`${this.baseUrl}/editedData` , data);
  }
}

我要使用的Laravel后端路由

Route::put('editedData/{id}', 'SubmitFormController@updateData');

Laravel后端控制器

 public function updateData(Request $request){
        dd($request->all());
    }

1 个答案:

答案 0 :(得分:1)

由于您是从Angular前端发出发布请求,而Laravel的路线仅对PUT方法起反应,因此您需要在Angular前端中添加一个_method字段,其值为PUT使Laravel可以将其作为PUT的请求来掌握。参见form-method-spoofing

因此,在Angular前端中添加以下内容:

 const capture = {
      UserName: formValue.value.childName,
      Password: formValue.value.childAge,
      Gender: formValue.value.childGender,
      _method : 'PUT'
 };

话虽如此,您也可以尝试使用Angular HttpClient提供的PUT方法。您很可能会避免上述设置,而只需执行this.http.put(//your code).