2

我正在更新一个属于某个位置的租赁,我可以填充和持久化租赁数据,但是当我尝试保存()、更新()或推送()时,当我填充通过预先加载检索到的位置关系数据时;它不会持久化到数据库中。根据文档 push() 应该可以工作,因为它显然会保持关系。

更新操作

public function update($id, RentalUpdateRequest $request)
{
    // Eager load the rental with relationships
    $Rental = Rental::with('location')->find($id);

    // Retrieve rental data from request, and fill
    $requestData = $request->only('stall', 'level', 'description');
    $Rental->fill($requestData);

    // Retrieve rental location data from request, and fill
    $requestData = $request->only(
        'location.street_address',
        'location.city',
        'location.province',
        'location.country',
        'location.postal_code'
    );
    $Rental->location->fill($requestData);

    // Persist rental to database, as well as all relationships
    $Rental->update();

    // Return rental data for capture by AngularJS interceptor
    return response()->json([
        'rental' => $Rental,
        'action' => 'rental_updated',
        'message' => trans('rental.updated')
    ]);
}

Payload 和 Eager Loaded 数据的格式相同

在客户端加载的原始租金

rental: 
    description: "This is a rental."
    id: 1
    level: "7"
    location: 
        city: "Victoria"
        country: "Canada"
        id: 11
        postal_code: "V8T 2L5"
        province: "British Columbia"
        street_address: "180 Blanshard Dr."
    region_id: 1
    rental_location_id: 11
    stall: "7"
    user_id: 1

如果有效载荷是:

rental: 
    description: "This is a rental."
    id: 1
    level: "9" <-- Changed
    location: 
        city: "asdf" <-- Changed
        country: "asdf" <-- Changed
        id: 11
        postal_code: "V8T 2L5"
        province: "asdf" <-- Changed
        street_address: "180 Blanshard Dr."
    region_id: 1
    rental_location_id: 11
    stall: "7"
    user_id: 1

Save()、Update() 或 Push() 之后的响应

rental: 
    description: "This is a rental."
    id: 1
    level: "9" <-- Persisted
    location: 
        city: "Victoria" <-- NOT Persisted
        country: "Canada" <-- NOT Persisted
        id: 11
        postal_code: "V8T 2L5"
        province: "British Columbia" <-- NOT Persisted
        street_address: "180 Blanshard Dr."
    region_id: 1
    rental_location_id: 11
    stall: "7"
    user_id: 1

似乎在 Laravel 5.1 中 push() 正在从 API 中删除,不再在 5.1 文档中,但是 save() 和 update() 也不起作用,有什么建议吗?

我现在已经多次遇到这种情况,我尝试设置急切加载的关系的属性,但只有顶级键:值对被持久化,而子关系对则不被持久化。解决这个问题的唯一方法似乎是单独获取位置并保存(),但是如果我无法保存关系,那么像这样急切加载有什么意义,并且必须向数据库发出另一个请求。

4

0 回答 0