简体   繁体   中英

Laravel, sync data with Pivot

Laravel 5.3, I have this 2 models:

User:

public function newFunctions()
{
    return $this
        ->belongsToMany('App\NewFunctions', 'user_newfunctions')
        ->withPivot(['function_count', 'days_count']);
}

NewFunctions:

public function users()
{
    return $this
        ->belongsToMany('App\User', 'user_newfunctions', 'new_function_id', 'user_id')
        ->withPivot(['function_count', 'days_count']);
}

I now how can I save new data to User, with this:

$user = User::findOrFail($id);
$user->name = $request->input('name');
$user->save();

But now I have to update some values of a pivot table. the pivot table is this:

user_id | new_functions_id | function_count | days_count
---------------------------------------------------------
    814 |           1      |   5            |2019-07-19 12:26:19
    814 |           3      |   7            |2019-07-19 12:26:19

I have more than 1 row per user_id . I was trying to use:

$user
    ->newFunctions()
    ->sync([
        'days_count' => $test_date,
        'function_count' => $test_int_number
    ]);

But I'm getting error like:

Ilegal offset type

because is trying to update with this:

array(
    'records' => array(
        'days_count' => object(Carbon), 'function_count' => '66'),
        'results' => array(),
        'id' => object(Carbon),
        'attributes' => array()

    )
)

in BelongsToMany.php

So:

  • How could I update the values for each user_id on the pivot table?
  • And how should use sync to update just 'function_count' and 'days_count'? they come from request.

->sync() isn't used like that; it's used to attach() and detach() related new_function_ids until only the ids in sync() are present. You're probably looking for updateExistingPivot()

An example of ->sync() would be using the array:

$user->newFunctions()->sync([
    "new_function_id" => 1,
    "function_count" => 6,
    "days_count" => "2019-07-08 12:00:00",
]);

This would remove the record where new_function_id is 3 , and updating the values where new_function_id is 1 .

To update function_count and days_count for either new_function_id of 1 or 3 , use ->updateExistingPivot() (pass the id you want to update as the first parameter):

$user
    ->newFunctions()
    ->updateExistingPivot("1", [
        "function_count" => 6,
        "days_count" = "2019-07-08 12:00:00"
    ]);
// or $user->newFunctions()->updateExistingPivot("3", ...);

This will update the pivot table where new_function_id is 1 , while leaving the row where new_function_id is 3 .

Edit: If you're looking to update all existing records in the pivot table, you'll need to do this in a loop, call a sync with all current records in a single array, or run a manual query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM