Laravel 查询构建 2 table 问题连接问题(已解决)

Laravel Query Build 2 table problem join questions (Dissolved)

我在 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
    }

感谢您对安德鲁·拉森的关注

迁移顺序

      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('App\Models\Pool','pool_id','id');
  }
  function getTestgroup(){
    return $this->hasOne('App\Models\Testgroup','id','grup_id');
  }

  public function user()
  {
      return $this->belongsTo('App\User', 'user_id');
  }

  public function getDetay()
  {
    return $this->hasMany('App\Models\Orderdetail','order_id')
        ->where('statu_id','<=','2')->where('finish','<=','0')->first();
  }
}

型号订单详情

class Orderdetail extends Model
{
  public function getOrder(){
    return $this->belongsTo('App\Models\Order','order_id');
  }

  public function getPool(){
    return $this->hasOneThrough(
      'App\Models\Pool',
      'App\Models\Order',
      '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('App\Models\Orderdetail', 'order_id');
}

此代码应与您的代码执行相同的操作,除了此代码使用 eloquent 而不是查询构建器(代码未测试):

$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 被定义为字符串,但您正在使用它们,就好像它们是整数一样。如果您只需要数值,您应该将它们定义为整数。