Closed Smoggert closed 4 years ago
Does it work if you assign an auto-incrementing primary key to your pivot model table?
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Shoppingcartorder extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shoppingcartorder', function (Blueprint $table) {
$table->timestamps();
$table->id();
$table->foreignId('product_id');
$table->foreignId('user_id');
$table->integer('amount');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shoppingcartorder');
}
}
Adjusted my migration to include an id()
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
Added this to the pivot model.
Still increment() does not function.
When I dd the resulting pivot.
public function incrementAmount(Product $product, Int $amount = 0)
{
$pivot = $product->users()->where('user_id', Auth::id())->first()->pivot;
$pivot->increment('amount', $amount);
dd($pivot);
}
The DD data that is different between the 2 functions: <<< increment('column', Int amount)
#original: array:5 [▼
"product_id" => "7"
"user_id" => "2"
"amount" => 2
"created_at" => "2020-09-27 22:02:47"
"updated_at" => "2020-09-27 22:02:47"
]
#changes: array:1 [▼
"amount" => 2
]
<<< regular selfwritten function
#original: array:5 [▼
"product_id" => "7"
"user_id" => "2"
"amount" => 1
"created_at" => "2020-09-27 22:02:47"
"updated_at" => "2020-09-27 22:02:47"
]
The culprit is the custom saving function written and returned in Illuminate\Database\Eloquent\Model::class: protected function incrementOrDecrement($column, $amount, $extra, $method) L:632
return tap($query->where(
$this->getKeyName(), $this->getKey()
)->{$method}($column, $amount, $extra), function () use ($column) {
$this->syncChanges();
$this->fireModelEvent('updated', false);
$this->syncOriginalAttribute($column);
}
Specifically
$this->syncOriginalAttribute($column);
I'm unaware why this creates issues with custom pivot columns (it might have issues with other columns too, cba to check)
This can be alleviated by calling the inherent save function of the model ! (after all that's what it's there for !)
return tap($query->where(
$this->getKeyName(), $this->getKey()
)->{$method}($column, $amount, $extra), function () use ($column) {
$this->save();
});
Whether or not this breaks some other code that this dodgy custom save is based on is beyond me. But this works perfectly for me.
I see the problem. Fixed in next patch.
Description:
$custom_pivot->increment('columname', value); does not update database with incremented value
Steps To Reproduce:
The product model:
The User Model: (jetstream user model)
The pivot
The function I was trying to use in my pivot's controller
Either with or without save() This function DOES increment the pivot model. dd() shows amount as X + $amount. But the database is unaffected.
So I rewrote the incrementing function myself, and this works. I checked out the incrementing() function in Model to see why the behavior is different but it does a bunch of abstractions and calls that just confuse me.
If this is intended behavior, sorry for the time wasted. This is incredibly confusing.