Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel4 update additional columns in pivot table

I'm trying to update additional column data in a pivot table in a many to many relationship.

I have two tables - reservation and resource linked with a pivot table. I can attach and am working with the model. However I'm struggling to update one of the additional columns in the pivot table.

I have an object: '$reservation' From that object I created another object $resources using:

$resources = $reservation->resource()->get();

I'm then iterating through $resources using a foreach loop as follows

foreach($resources as $resource ) {...}

I then want to update a column called gcal_id and am using the following:

$resource->pivot->gcal_id = "TEST";
$resource->save();

If I var_dump the model I can see the property exists to the correct value but in the database itself the entry is not being updated - so the save is not working

I have the columns listed in both sides of the relationship with this:

->withPivot('start', 'end', 'quantity', 'product_id','gcal_id')

Given I have the resource object how can I update a column correctly in the pivot table and save to database?

Thanks

like image 398
Ray Avatar asked Sep 29 '13 21:09

Ray


People also ask

How to add additional columns to a pivot table?

Add an Additional Row or Column FieldClick any cell in the PivotTable. The PivotTable Fields pane appears. You can also turn on the PivotTable Fields pane by clicking the Field List button on the Analyze tab. Click and drag a field to the Rows or Columns area.

How to add more lines to a pivot table?

In the PivotTable, select the item you want. This displays the PivotTable Tools tab on the ribbon. On the Design tab, in the Layout group, click Blank Rows, and then select the Insert Blank Line after Each Item Label or Remove Blank Line after Each Item Label check box.


1 Answers

After that you set the attribute on the pivot:

$resource->pivot->gcal_id = "TEST";

You seem to save the resource and not the pivot:

$resource->save();

If you want the pivot to be saved, saving the resource is not enough. Call save on the pivot instead:

$resource->pivot->gcal_id = "TEST";
$resource->pivot->save();
like image 115
Matteus Hemström Avatar answered Oct 31 '22 17:10

Matteus Hemström