首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >价值最大化在雄辩数据库中的检索

价值最大化在雄辩数据库中的检索
EN

Stack Overflow用户
提问于 2018-12-07 02:47:07
回答 1查看 95关注 0票数 0

如何在我的API控制器中将这个原始SQL转换为雄辩的数据库?

我想要从API控制器的两个表中获取数据。

首先: Formc_image表,它是关于数据的一般信息。

第二,Formc_image_detail,它是数据的细节和路径。

它们是由ID关联的。

这是我的API控制器FormCController.php

代码语言:javascript
运行
复制
SELECT * 
FROM formc_image_detail
WHERE id_tps=$id_tps AND jenis_pemilihan=$election_id
AND versi = (SELECT MAX(versi) FROM formc_image WHERE id_tps=id_tps AND jenis_pemilihan=$election_id)
ORDER BY no_lembar ASC

这是我的模型FormCImageDetail

代码语言:javascript
运行
复制
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class FormCImageDetail extends Model
{
    protected $table = 'formc_image_detail';

    public function formc_image(){
      return $this->belongsTo('App\Models\FormCImage');
    }
}

这是我的FormCImage模型

代码语言:javascript
运行
复制
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class FormCImage extends Model
{
    protected $table = 'formc_image';
}

我在API控制器中编写了以下代码:

代码语言:javascript
运行
复制
return response(FormCImageDetail::with('formc_image')
      ->where('jenis_pemilihan', $electionId)
      ->where('id_tps', $tpsId)
      ->orderBy('no_lembar', 'ASC')->paginate(100)->jsonSerialize(), Response::HTTP_OK);

但还是出错了。

这是我的移民:

代码语言:javascript
运行
复制
Schema::create('formc_image', function (Blueprint $table) {
            $table->integer('id_tps');
            $table->smallint('versi');
            $table->string('jenis_pemilihan');
            $table->timestamps();
}

Schema::create('formc_image_detail', function (Blueprint $table) {
            $table->integer('id_tps');
            $table->smallint('versi');
            $table->integer('no_lembar');
            $table->string('jenis_pemilihan');
            $table->char('url_image', 100);
            $table->timestamps();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-07 03:08:53

用这个:

代码语言:javascript
运行
复制
return FormCImageDetail::with('formc_image')
    ->where('jenis_pemilihan', $electionId)
    ->where('id_tps', $tpsId)
    ->where('versi', function($query) use($election_id) {
        $query->select(DB::raw('max(versi)'))
            ->from('formc_image')
            ->whereColumn('id_tps', 'formc_image_detail.id_tps')
            ->where('jenis_pemilihan', $election_id);
    })
    ->orderBy('no_lembar')
    ->paginate(100);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53662530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档