【问题标题】:Search by Max of Value in Eloquent Database Laravel在 Eloquent 数据库 Laravel 中按最大值搜索
【发布时间】:2018-12-07 02:47:07
【问题描述】:

如何在我的 API 控制器中将此原始 SQL 转换为 Eloquent 数据库?

我尝试的是从我的 API 控制器的两个表中获取数据。

第一:formc_image表,是关于数据的一般信息

第二个:formc_image_detail 是数据的细节和路径

它们通过 ID 相关。

这是我的 API 控制器 FormCController.php

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

<?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 模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

我在我的 API 控制器中编写了这段代码:

return response(FormCImageDetail::with('formc_image')
      ->where('jenis_pemilihan', $electionId)
      ->where('id_tps', $tpsId)
      ->orderBy('no_lembar', 'ASC')->paginate(100)->jsonSerialize(), Response::HTTP_OK);

但还是报错。

这是我的迁移:

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();
}

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我更新了我的详细问题。请帮我解决这个问题@JonasStaudenmeir

标签: sql database laravel eloquent


【解决方案1】:

使用这个:

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);

【讨论】:

  • 感谢您帮助我。但它仍然是错误的。调试是本地的。错误:类 Illuminate\Database\Eloquent\Builder 的对象无法转换为字符串。 @JonasStaudenmeir
  • 运行代码时仍然存在错误。调试是本地的。错误:SQLSTATE [42703]:未定义的列:7 错误:列 formc_image.id 不存在。 @JonasStaudenmeir
  • 我只是在我的 API 控制器 @JonasStaudenmeir 中运行你的 SQL 解决方案
  • [2018-12-07 03:49:46] local.ERROR: SQLSTATE[42703]: Undefined column: 7 ERROR: column formc_image.id does not exist LINE 1: select * from "formc_image " where "formc_image"."id" in ($1) ^ (SQL: select * from "formc_image" where "formc_image"."id" in ()) {"userId":6,"email":"davidwinalda94@gmail .com","exception":"[object] (Illuminate\\Database\\QueryException(code: 42703): SQLSTATE[42703]: Undefined column: 7 ERROR: column formc_image.id 不存在
  • LINE 1: select * from \"formc_image\" where \"formc_image\".\"id\" in ($1) ^ (SQL: select * from \"formc_image\" where \" formc_image\".\"id\" in ()) 在 C:\\xampp7\\htdocs\\situng-web\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php :664, PDOException(code: 42703): SQLSTATE[42703]: Undefined column: 7 ERROR: column formc_image.id 不存在
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 2019-08-13
  • 1970-01-01
  • 2012-11-03
  • 2017-12-07
  • 2021-04-10
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多