Laravel Query Build 2表问题联接问题(已解决)



我在Laravel 中找不到如何做到这一点

我添加了我的表并查询

也许我选择了错误的方式,如果你能分享你的建议,我将非常高兴

table order
id      pool_id      durum     tarih
1       1            0         2020-02-24(not start)
2       2            1         2020-02-24(started)
3       1            1         2020-02-24(started)
4       2            0         2020-02-24(not start)
table orderdetail
id      order_id      statu_id     finish
1       2             1            1
2       2             2            0
3       3             1            1
4       3             2            1
5       3             3            0

laravel码;

$orders= Order::where('pool_id',$pool)
->leftJoin('orderdetails', function ($join) {
$join->on('orderdetails.order_id', '=', 'orders.id')
->where('durum','=','1')
->where('orderdetails.statu_id','<=','2')
->where('orderdetails.finish','=','0');
->where('durum','=','0')
->whereDate('tarih', '>', Carbon::now()->subDays(2)->toDateString())
->whereDate('tarih', '<', Carbon::now()->addDays(1)->toDateString())
->select('orders.*')
->distinct()
->get(); 

如何做如下

if(durum == 1){
join and condition (statu_id and finish) success get only order row
}
if(durum == 0){
get only order row
}

感谢您的兴趣Andrew Larsen

迁移顺序

Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('orderno')->unique();
$table->unsignedBigInteger('pool_id');
$table->foreign('pool_id')->references('id')->on('pools')->onDelete('cascade');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedBigInteger('grup_id');
$table->foreign('grup_id')->references('id')->on('testgroups');
$table->string('vardiya');
$table->string('tanim')->default(0);//0: plansız-1:planlı
$table->string('durum')->default(0);//0: beklemede - 1: işlem başladı - 2: işlem bitti - 3: iptal
$table->timestamps();
});

迁移订单详细信息

Schema::create('orderdetails', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade')->onDelete('cascade');
$table->unsignedBigInteger('test_id');
$table->foreign('test_id')->references('id')->on('tests');
$table->unsignedBigInteger('statu_id');
$table->foreign('statu_id')->references('id')->on('status');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('finish')->default(0);
$table->timestamps();
});

型号订单

class Order extends Model
{
function getPool(){
return $this->belongsTo('AppModelsPool','pool_id','id');
}
function getTestgroup(){
return $this->hasOne('AppModelsTestgroup','id','grup_id');
}
public function user()
{
return $this->belongsTo('AppUser', 'user_id');
}
public function getDetay()
{
return $this->hasMany('AppModelsOrderdetail','order_id')
->where('statu_id','<=','2')->where('finish','<=','0')->first();
}
}

型号订单详细信息

class Orderdetail extends Model
{
public function getOrder(){
return $this->belongsTo('AppModelsOrder','order_id');
}
public function getPool(){
return $this->hasOneThrough(
'AppModelsPool',
'AppModelsOrder',
'id', // Foreign key on users table...
'id', // Foreign key on history table...
'order_id', // Local key on suppliers table...
'pool_id' // Local key on users table...
);
}
}

感谢大家

如果您将此函数添加到订单模型中:

public function orderDetails() {
return $this->hasMany('AppModelsOrderdetail', 'order_id');
}

这段代码应该和你的代码做得一样,只是这段代码使用了雄辩而不是查询生成器(代码没有经过测试(:

$orders = Order::where([
['pool_id', $pool],
['tarih', '>', Carbon::now()->subDays(2)->toDateString()],
['tarih', '<', Carbon::now()->addDays(1)->toDateString()]
])
->where(function($q) {
$q->where(function($q2) {
$q2->where('durum', 1)
->whereHas('orderDetails', function($q3) {
$q3->where([
['statu_id', '<=', 2],
['finish', 0]
]);
});
})
->orWhere('durum', 0);
})
->get();

同样在迁移文件中,列durumfinish被定义为字符串,但您将它们当作整数来使用。如果只需要数值,则应将其定义为整数。

最新更新