Я уверен, что упускаю что-то простое здесь, но все примеры, которые я могу найти, похоже, не справляются с работой.
Короче говоря, есть цены на «предложение» вТаблица. Если с параметрами нет цены, создайте запись (эта часть работает), если нет, проверьте, не дешевле ли новая цена, обновите ли запись.
Вот мой код:
$priceExist = OfferPrice::
where('offer_id', $offer_id, 'AND')
->where('personal', $price['personal'], 'AND')
->where('status', 1, 'AND')
->where('deposit', $price['deposit'], 'AND')
->where('duration', $price['duration'], 'AND')
->where('mileage', $price['mileage'])->get()->first();
if (!$priceExist) {
$offerPrice = new OfferPrice($price);
$offerPrice->save();
} else if ( $price['price'] < $priceExist->price) {
$priceExist->price = $price['price'];
$priceExist->save();
}
В настоящее время возвращается ошибка:
ErrorException: недопустимый тип смещения в /vendor/illuminate/database/Eloquent/Model.php:775
Похоже, что это вызвано $priceExist->save();
.
Любые идеи?
Я должен подчеркнуть, что приведенный ниже код работает, но, очевидно, это плохой код, вызывающий одну и ту же запись дважды:
$priceExist = OfferPrice::
where('offer_id', $offer_id, 'AND')
->where('personal', $price['personal'], 'AND')
->where('status', 1, 'AND')
->where('deposit', $price['deposit'], 'AND')
->where('duration', $price['duration'], 'AND')
->where('mileage', $price['mileage'])->get()->first();
if (!$priceExist) {
$offerPrice = new OfferPrice($price);
$offerPrice->save();
} else if ( $price['price'] < $priceExist->price) {
OfferPrice::
where('offer_id', $offer_id, 'AND')
->where('personal', $price['personal'], 'AND')
->where('status', 1, 'AND')
->where('deposit', $price['deposit'], 'AND')
->where('duration', $price['duration'], 'AND')
->where('mileage', $price['mileage'])
->update(['price' => $price['price']]);
}