首页 > 解决方案 > 如何在 OctoberCMS 的联接表中使用 where 子句?

问题描述

我在 Laravel 5 中使用 OctoberCMS。我有几个模型,需要加入它们。

我有“PaymentData”模型和“AppsData”模型。他们都有“mem_id”列,我想根据“PaymentData”模型的数据从“AppsData”模型中获取“app_process”列的值。

我在下面尝试的会导致此错误消息。

“SQLSTATE [42S22]:未找到列:'where 子句'中的 1054 未知列 'app_process'(SQL:选择计数(*)作为聚合 from BYAPPS_apps_payment_datawhere pay_type!= 1 and amount= 0 and app_process!= 8)”在 / 的第 664 行主页/ljw/public_html/byapps_cms/vendor/laravel/framework/src/Illuminate/Database/Connection.php

支付数据模型

class PaymentData extends Model
{
    use \October\Rain\Database\Traits\Validation;

    public $table = 'BYAPPS_apps_payment_data';

    public $rules = [
    ];

    public $hasOne = [
      'joins' => [
        'Jiwon\Byapps\Models\AppsData',
        'key' => 'mem_id',
        'otherKey' => 'mem_id'
      ]
    ];
}

应用数据模型

class AppsData extends Model
{
    use \October\Rain\Database\Traits\Validation;

    public $table = 'BYAPPS_apps_data';

    public $rules = [
    ];
}

我在 home.htm 中尝试过的内容

use Jiwon\Byapps\Models\AppsData;
use Jiwon\Byapps\Models\PaymentData;

function onGetAppChartData()
{

  $this['appsFree'] = PaymentData::where('pay_type', '!=', '1')
                      ->where('amount', '=', '0')
                      ->where('app_process', '!=', '8')
                      ->count();
}

我在这里做错了什么?请有人帮助我...!_!

标签: phpmysqllaraveloctobercms

解决方案


您可以尝试编写这样的自定义查询:

$this['appsFree'] =   \DB::table('BYAPPS_apps_payment_data as p')
            ->join('BYAPPS_apps_data as a', 'a.mem_id', '=', 'p.mem_id')
            ->where('p.pay_type', '!=', '1')
            ->where('p.amount', '=', '0')
            ->where('p.app_process', '!=', '8')
            ->count();

推荐阅读