jeudi 22 mars 2018

belongsToMany collection empty during Model retrieved event

Background - I'm creating a system where administrators can create arbitrary fields, which are then combined into a form. Users then complete this form, and the values input against each field are stored in a table. However, rather than overwrite the previous value, I plan on keeping each past value as individual rows in the table. I then want to be able to display the contents submitted in each form, but only the most recently submitted value.

Problem

I have a model, Service, that features a belongsToMany relationship with another model, Field. This relationship is defined as:

public function fields()
{
    return $this->belongsToMany('App\Field')->withPivot('id', 'value', 'date')->withTimestamps();
}

The intermediary table has 3 values I wish to retrieve, id, value and date.

A Service may have 1 or more Fields, and for each field it may also have more than 1 pivot row. That is, a single Service/Field pairing may have multiple entries in the pivot table with different pivot values. For example:

Table field_service

id      | service_id | field_id | value   | date
------------------------------------------------------
1       | 1          | 1        | lorem   | 2018-02-01
2       | 1          | 1        | ipsum   | 2018-01-01
3       | 1          | 1        | dolor   | 2017-12-01
4       | 1          | 2        | est     | 2018-03-10
5       | 1          | 2        | sicum   | 2018-03-09
6       | 1          | 2        | hoci    | 2018-03-08

What I want is to get either:

  • A specific row from the pivot table for each Field associated with the Service, or
  • A specific value from the pivot table for each Field associated with the Service.

For example - in the table above, I would like the Service with ID 1 to have 3 Fields in the relationship, with each Field containing an attribute for the corresponding pivot value. The Fields attached would be specified by the corresponding pivot table entry having the most recent date. Something akin to:

$service->fields()[0]->value = "lorem"
$service->fields()[1]->value = "est"

I feel there's an obvious, 'Laravel'ly solution out there, but it eludes me...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire