将原始sql查询转换为laravel eloquent模型

时间:2018-03-19 12:56:31

标签: laravel-5 eloquent

我一直在尝试将以下查询转换为雄辩的模型查询/原始数据库查询。我对laravel知之甚少,而且我已经停下来了。

 select 
                status, 
                (SELECT count(*) FROM active_call_statuses WHERE active_call_id = 'CA3a77245ab0eac10f8cf3aa8e7c8f9a91') AS total 
            from 
                active_call_statuses
            left JOIN
                active_calls ON active_calls.parent_call_id = active_call_statuses.active_call_id
            where 
                status IN('in-progress', 'completed', 'ringing', 'answered', 'busy', 'failed', 'no-answer')
            and 
                active_calls.parent_call_id = 'CA3a77245ab0eac10f8cf3aa8e7c8f9a91'

查询的目的是选择具有给定呼叫状态的所有列,并通过子查询中的id计算与当前呼叫关联的状态条目的总数。

尽管我可以看到查询在mysql中执行的操作,但我不知道如何将其转换为雄辩的查询。

active_call_statuses表和active_calls表分别通过eloquent中parent_call_id上的一对多关系相互链接。

class ActiveCall extends Model

{
    /**
     * @var mixed
     */
    public $timestamps = false;

    /**
 * @var array fillable properties
 */
protected $fillable = ['user_id', 'conference_id', 'parent_call_id'];

/**
 * @return mixed
 */
public function statuses()
{
    return $this->belongsToMany('app\ActiveCallStatus');
}
}



  class ActiveCallStatus extends Model
{
/**
 * @var bool timestamps enabled
 */
public $timestamps = false;

/**
 * @var array fillable properties
 */
protected $fillable = ['active_call_id', 'user_id', 'status'];

/**
 * @return mixed
 */
public function activeCall()
{
    return $this->belongsTo('app\ActiveCall');
}
}

我已经尝试在DB::select中使用DB::raw包装查询,通过将参数绑定到DB->table(...)->selectRaw(...) .etc来调用带有selectRaw aliased with :id方法的表,但是一切都导致数据库错误invalid parameter number或其他错误。

这是我的最后一次尝试:

processedUsers = DB::table('active_call_statuses')->select(
                    DB::raw("
                        SELECT
                        user_id,
                        status,
                        (SELECT count(*) FROM active_call_statuses WHERE active_call_id = :id) AS total 
                    FROM 
                        active_call_statuses"),
                    ['id' => $activeCall->parent_call_id])
                ->whereIn('status',"('in-progress', 'completed', 'ringing', 'answered', 'busy', 'failed', 'no-answer')")
                ->where("active_calls.parent_call_id", $activeCall->parent_call_id);

导致:

[2018-03-19 12:33:45] local.ERROR: Invalid argument supplied for foreach() {"exception":"[object] (ErrorException(code: 0): Invalid argument supplied for foreach() at C:\\wamp64\\www\\Stage\\LanthopusX\\voip\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php:763)

1 个答案:

答案 0 :(得分:0)

试试这个:

ActiveCallStatus::leftJoin('active_calls', 'active_calls.parent_call_id', '=', 'active_call_statuses.active_call_id')
    ->whereIn('status', ['in-progress', 'completed', 'ringing', 'answered', 'busy', 'failed', 'no-answer'])
    ->where('active_calls.parent_call_id', $activeCall->parent_call_id)
    ->select('status')
    ->selectRaw('(SELECT count(*) FROM active_call_statuses WHERE active_call_id = ?) AS total', [$activeCall->parent_call_id])
    ->get();