【问题标题】:Laravel where condition - pgsql queryLaravel where 条件 - pgsql 查询
【发布时间】:2021-09-18 15:44:02
【问题描述】:

我想选择MaxFileSize列中的值,同一张表中FileType列的值等于$fileType。也就是说,在这个例子中,如果我输入的 uploadType 是 mp4 ,我需要得到结果(即 $maxSize )为 6。

$fileType=$request->uploadType;
$maxSize = Filesizes::where('FileType',$fileType)->get(['MaxFileSize']);

我已尝试上述代码,但无法使用此代码获得我想要的结果。请帮助我提出您的建议。

文件大小表

MaxFileSize FileType
6 mp4

上面的代码给出了这样的输出:

Illuminate\Database\Eloquent\Collection {#1313
  #items: array:1 [
    0 => App\Models\Filesizes {#1314
      #table: "filesizes"
      #fillable: array:1 [
        0 => "MaxFileSize"
      ]
      #connection: "pgsql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:1 [
        "MaxFileSize" => 6
      ]
      #original: array:1 [
        "MaxFileSize" => 6
      ]
      #changes: []
      #casts: array:1 [
        "deleted_at" => "datetime"
      ]
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
      #forceDeleting: false
    }
  ]
}

【问题讨论】:

  • 什么是错误和你想要实现什么。所以有人可以帮助你
  • @JohnLobo 请查看更新后的问题。我已经添加了我正在输入的输出。对于我给出的示例,如果我输入的 uploadType 是 mp4 ,我需要得到结果(即 $maxSize )为 6。
  • $maxSize = Filesizes::where('FileType',$fileType)->get(['MaxFileSize','FileType']);
  • 如果你想读取格式然后 dd($maxSize->toArray());
  • @JohnLobo 我试过这个 - dd($maxSize->toArray()); 。但它显示了这样的输出 - array:1 [ 0 => array:1 [ "MaxFileSize" => 6 ] ] 。但是,我需要的输出只是 6。

标签: database laravel postgresql laravel-8 rest


【解决方案1】:

要直接从一个查询中获取值,请使用方法value()

$maxSize = Filesizes::where('FileType',$fileType)->value('MaxFileSize');

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 2014-10-04
    • 2019-08-07
    • 2015-09-26
    • 1970-01-01
    • 2020-08-15
    • 2021-12-02
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多