我要选择所有未满足条件的要约(OffersProof上的数据不存在,或者OfferProof中的多个条件,要约状态为0)
列出没有证据的报价。如果存在证明,则检查其状态。如果该值为0,则将其显示在列表中。
优惠模式
class Offers extends Model
{
protected $table = 'offers';
protected $fillable = [ "name","desc"];
protected $hidden = [];
}
提供证明模型
class OffersProof extends Model
{
protected $table = 'offers_proof';
protected $fillable = [ "offer", "user","pub","time","status" ];
protected $hidden = [];
}
这是我尝试过的查询。选择中存在问题。不确定此功能是否完美,或需要实现使用关系。
$user = "test";
$pub = "demo";
$offers = Offers::leftJoin('offers_proof', function($leftJoin)use($user,$pub)
{
$leftJoin->on('offers_proof.offer', '=', 'offers.id');
$leftJoin->where('offers_proof.user', '=', $user);
$leftJoin->where('offers_proof.pub', '=', $pub);
$leftJoin->where('offers_proof.status', '!=', 0);
})->where($conditions)->get();
此代码返回所有报价。我要过滤在OfferProof表上没有证明的报价。
是否有更好的方法来查询性能良好的数据,以及如何根据上述要求选择商品。
答案 0 :(得分:0)
如果我对您的理解正确,则希望选择不完整的要约或要约证明状态为0。我认为此代码应该有效。
$user = "test";
$pub = "demo";
$offers = Offers::leftJoin('offers_proof', 'offer', 'offers.id')
->select(*)
->where('offers_proof.user', $user)
->where('offers_proof.pub', $pub)
->where('offers_proof.status', 0)
->where($conditions)
->get();
答案 1 :(得分:0)
我错过了DB :: Raw部分和一种检查左联接数据是否存在的条件。
$user = "test";
$pub = "demo";
$offers = Offers::leftJoin('offers_proof', function($leftJoin)use($user,$pub)
{
$leftJoin->on('offers_proof.offer', '=', 'offers.id');
$leftJoin->where('offers_proof.user', '=', DB::raw("'$user'"));
$leftJoin->where('offers_proof.pub', '=', DB::raw("'$pub'"));
$leftJoin->where('offers_proof.status', '!=', DB::raw("0"));
})->where('offers_proof.user', '=', NULL)->where($conditions)->get();