简体   繁体   English

在Laravel Select查询中获取其他数据

[英]Get additional data in laravel select query

When i use select query in laravel i am given additional data. 当我在laravel中使用选择查询时,会得到其他数据。

My query 我的查询

MyModal::where('state', 1)
        ->select('code as popupContent', 'latitude as lat','longitude as lng')
        ->take(3)
        ->get()
        ->toArray();

Returned data: 返回的数据:

[
  [
    "popupContent" => "260563"
    "lat" => 35.765014
    "lng" => 51.333209
    "update_time" => null
    "rooms_number" => null
  ]
  [
    "popupContent" => "891962"
    "lat" => 35.794741
    "lng" => 51.391965
    "update_time" => null
    "rooms_number" => null
  ]
  [
    "popupContent" => "477845"
    "lat" => 35.723649
    "lng" => 51.320682
    "update_time" => null
    "rooms_number" => null
  ]
]

In the code above, fields update_time and rooms_number are added 在上面的代码中,添加了字段update_timerooms_number

I want to give this data: 我想提供以下数据:

[
  [
    "popupContent" => "260563"
    "lat" => 35.765014
    "lng" => 51.333209
  ]
  [
    "popupContent" => "891962"
    "lat" => 35.794741
    "lng" => 51.391965
  [
    "popupContent" => "477845"
    "lat" => 35.723649
    "lng" => 51.320682
  ]
]

You maybe override $append property 您可能会覆盖$append属性

public $appends = ['update_time', 'rooms_number'];

For hidden the appended columns use this 对于隐藏的附加列,请使用此

MyModal::where('state', 1)
    ->select('code as popupContent', 'latitude as lat','longitude as lng')
    ->take(3)
    ->get()
    ->makeHidden(['update_time', 'rooms_number'])
    ->toArray();

I think it's still in your model. 我认为它仍在您的模型中。 You can try: 你可以试试:

$items =  DB::table('tableName')
->where('state', 1)
->select('code as popupContent', 'latitude as lat','longitude as lng')
->take(3)->get();

You can use the following code: 您可以使用以下代码:

    YourModel::select('code as popupContent', 'latitude as lat','longitude as lng')
    ->where('state', 1)
    ->take(3)
    ->get()
    ->toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM