我想用一个位置更新我模型中的一列。但是,我不确定如何执行此操作。这是我的下面查询
transition-delay: .4s
销售模式
$unused = Sale::whereHas('salesdetails',function($query) {
$query->where('product_id', '<=', 24)->where('switch', 1);
})->where(DB::raw("(DATE_FORMAT(transaction_date,'%Y-%m-%d'))"), '<', $now)->update(['switch' => 0]);
详细销售模式
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sale extends Model
{
protected $table = 'sales';
protected $primaryKey = 'id';
public $timestamps = false;
public function user()
{
return $this->belongsTo('App\User', 'member_id', 'id');
}
public function guest()
{
return $this->belongsTo('App\Guest', 'guest_id', 'id');
}
public function salesdetails()
{
return $this->hasOne('App\Sales_details','sales_id', 'id');
}
public function discount()
{
return $this->hasOne('App\Discount','id', 'discount_id');
}
}
在上面的查询中,我想将所有开关从salesdetails关系更新为“ 0”。我该怎么办?
答案 0 :(得分:0)
您要尝试的是错误的,您应该这样尝试
XmlDocument
我假设Sale::where(DB::raw("(DATE_FORMAT(transaction_date,'%Y-%m-%d'))"), '<', $now)->salesdetails()->where('product_id', '<=', 24)->where('switch', 1)->update(['switch' => 0]);
列在transaction_date
模型中。
答案 1 :(得分:0)
由于您要更新SalesDetails而不是Sale,因此我会反向查询,以便更新实际上在SalesDetails而不是Sale上:
Sales_details::whereHas('sale', function($query) use ($now){
$query->where(DB::raw("(DATE_FORMAT(transaction_date,'%Y-%m-%d'))"), '<', $now)
})->where('product_id', '<=', 24)->where('switch', 1)->update(['switch' => 0]);