This question already has an answer here:
这个问题在这里已有答案:
- How I can put composite keys in models in Laravel 5? 8 answers
如何在Laravel 5中将复合键放入模型中? 8个答案
I'm trying to update Model which has two primary keys.
我正在尝试更新具有两个主键的Model。
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
/**
* The table associated with the model.
*/
protected $table = 'inventories';
/**
* Indicates model primary keys.
*/
protected $primaryKey = ['user_id', 'stock_id'];
...
Migration
Schema::create('inventories', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('stock_id')->unsigned();
$table->bigInteger('quantity');
$table->primary(['user_id', 'stock_id']);
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('restrict')
->onDelete('cascade');
$table->foreign('stock_id')->references('id')->on('stocks')
->onUpdate('restrict')
->onDelete('cascade');
});
This is code which should update Inventory model, but it doesn't.
这是应该更新Inventory模型的代码,但它没有。
$inventory = Inventory::where('user_id', $user->id)->where('stock_id', $order->stock->id)->first();
$inventory->quantity += $order->quantity;
$inventory->save();
I get this error:
我收到此错误:
Illegal offset type
I also tried to use updateOrCreate() method. It doesn't work (I get same error).
我也尝试使用updateOrCreate()方法。它不起作用(我得到同样的错误)。
Can anyone tell how Model with two primary key should be updated?
任何人都可以告诉我们应该如何更新具有两个主键的模型?
2 个解决方案
#1
41
I've run into this problem a couple of times. You need to override some properties:
我曾经遇到过这个问题几次。您需要覆盖一些属性:
protected $primaryKey = ['user_id', 'stock_id'];
public $incrementing = false;
and methods (credit):
和方法(信贷):
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
$keys = $this->getKeyName();
if(!is_array($keys)){
return parent::setKeysForSaveQuery($query);
}
foreach($keys as $keyName){
$query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
}
return $query;
}
/**
* Get the primary key value for a save query.
*
* @param mixed $keyName
* @return mixed
*/
protected function getKeyForSaveQuery($keyName = null)
{
if(is_null($keyName)){
$keyName = $this->getKeyName();
}
if (isset($this->original[$keyName])) {
return $this->original[$keyName];
}
return $this->getAttribute($keyName);
}
I suggest putting those methods in a HasCompositePrimaryKey
Trait so you can just use
it in any of your models that need it.
我建议将这些方法放在HasCompositePrimaryKey Trait中,这样你就可以在需要它的任何模型中使用它。
#2
0
I've solved this by adding incrementing id and changing primaries to uniques.
我通过添加递增id并将原色更改为uniques来解决这个问题。
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('stock_id')->unsigned();
$table->bigInteger('quantity');
$table->unique(['user_id', 'stock_id']);
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('restrict')
->onDelete('cascade');
$table->foreign('stock_id')->references('id')->on('stocks')
->onUpdate('restrict')
->onDelete('cascade');
});
Also, I've removed from Model
此外,我已从模型中删除
protected $primaryKey = ['user_id', 'stock_id'];
In my opinion, this is not the best solution.
在我看来,这不是最好的解决方案。
#1
41
I've run into this problem a couple of times. You need to override some properties:
我曾经遇到过这个问题几次。您需要覆盖一些属性:
protected $primaryKey = ['user_id', 'stock_id'];
public $incrementing = false;
and methods (credit):
和方法(信贷):
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
$keys = $this->getKeyName();
if(!is_array($keys)){
return parent::setKeysForSaveQuery($query);
}
foreach($keys as $keyName){
$query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
}
return $query;
}
/**
* Get the primary key value for a save query.
*
* @param mixed $keyName
* @return mixed
*/
protected function getKeyForSaveQuery($keyName = null)
{
if(is_null($keyName)){
$keyName = $this->getKeyName();
}
if (isset($this->original[$keyName])) {
return $this->original[$keyName];
}
return $this->getAttribute($keyName);
}
I suggest putting those methods in a HasCompositePrimaryKey
Trait so you can just use
it in any of your models that need it.
我建议将这些方法放在HasCompositePrimaryKey Trait中,这样你就可以在需要它的任何模型中使用它。
#2
0
I've solved this by adding incrementing id and changing primaries to uniques.
我通过添加递增id并将原色更改为uniques来解决这个问题。
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('stock_id')->unsigned();
$table->bigInteger('quantity');
$table->unique(['user_id', 'stock_id']);
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('restrict')
->onDelete('cascade');
$table->foreign('stock_id')->references('id')->on('stocks')
->onUpdate('restrict')
->onDelete('cascade');
});
Also, I've removed from Model
此外,我已从模型中删除
protected $primaryKey = ['user_id', 'stock_id'];
In my opinion, this is not the best solution.
在我看来,这不是最好的解决方案。