Laravel firstOrNew有多种条件?

时间:2014-05-18 09:11:10

标签: php laravel-4

我有一个名为Stock的模型,我想插入或更新一行(如果它已经存在)。 该表有6列:

drink_id
flow_id
bought
left
created_at
updated_at

使用jQuery我收集数据,用ajax传递它。数据到达但以下代码无法正常运行:

$row = Stock::firstOrNew(array( 'drink_id'  => $drink_id,
                'flow_id'   => $max_flow));


$row -> bought = $bought;
$row -> left = $left;
$row -> save();

我收到以下错误:

{"error":
   {
    "type":"Illuminate\\Database\\QueryException",
    "message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id'
        in 'where clause' (SQL: update `stock` set `bought` = 22,
        `updated_at` = 2014- 05-18 09:10:14 where `id` is null)",
        "file":"C:\\wamp\\www\\csabi\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
        "line":539
   }
}

1 个答案:

答案 0 :(得分:4)

主要问题是Laravel不支持复合主键,因此我不得不在表中添加一个新的“id”行。