Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php API资源laravel筛选器数据_Php_Laravel - Fatal编程技术网

Php API资源laravel筛选器数据

Php API资源laravel筛选器数据,php,laravel,Php,Laravel,所以我尝试将一些数据传递到数组并发送到json。 但当我这么做的时候,我得到了很多信息: 以下是回应: { "current_page": 1, "data": [{ "id": 2, "object_id": 3, "booking_id": 1, "sender_id": 1, "receiver_id": 2, "message": "It is accepted", "type_id": 5, "read": 1,

所以我尝试将一些数据传递到数组并发送到json。 但当我这么做的时候,我得到了很多信息:

以下是回应:

{
  "current_page": 1,
  "data": [{
    "id": 2,
    "object_id": 3,
    "booking_id": 1,
    "sender_id": 1,
    "receiver_id": 2,
    "message": "It is accepted",
    "type_id": 5,
    "read": 1,
    "archive": 0,
    "star": 0,
    "created_at": "2019-02-26 11:45:28",
    "updated_at": "2019-02-26 12:15:11",
    "created_time": "26\/02\/2019",
    "host_user": 0,
    "guest_user": 1,
    "sender": {
      "id": 1,
      "first_name": "Joe",
      "last_name": "Cos",
      "email": "email-91@hotmail.com",
      "profile_image": null,
      "balance": 0,
      "status": "Active",
      "created_at": "2019-02-21 15:19:26",
      "updated_at": "2019-02-21 15:19:26",
      "profile_src": "http:\/\/xxx.com\/public\/images\/user_pic-225x225.png"
    },
    "bookings": {
      "id": 1,
      "object_id": 3,
      "code": "mYuL4p",
      "host_id": 1,
      "user_id": 2,
      "start_date": "2019-02-26",
      "end_date": "2019-02-27",
      "status": "Accepted",
      "guest": 0,
      "total_night": 1,
      "per_night": 20,
      "base_price": 20,
      "cleaning_charge": 0,
      "guest_charge": 0,
      "service_charge": 0,
      "security_money": 0,
      "host_fee": 0,
      "total": 20,
      "booking_type": "request",
      "currency_code": "EUR",
      "cancellation": "Flexible",
      "transaction_id": "67427302T32774838",
      "payment_method_id": 1,
      "accepted_at": "2019-02-26 11:45:28",
      "expired_at": null,
      "declined_at": null,
      "cancelled_at": null,
      "cancelled_by": null,
      "created_at": "2019-02-26 11:37:36",
      "updated_at": "2019-02-26 11:45:28",
      "host_payout": 23,
      "label_color": "success",
      "date_range": "Feb 26 - 27, 2019",
      "expiration_time": "2019\/02\/27 11:37:36",
      "currency": {
        "id": 3,
        "name": "Europe",
        "code": "EUR",
        "symbol": "€",
        "rate": "0.88",
        "status": "Active",
        "default": "0",
        "org_symbol": "€"
      }
    },
    "object_address": {
      "id": 3,
      "object_id": 3,
      "address_line_1": "XXXXXXXXX, 4050-352 Porto, Portugal",
      "address_line_2": null,
      "latitude": "49.999",
      "longitude": "-8.88810419921",
      "city": "P",
      "state": "P",
      "country": "P",
      "postal_code": "4050-352"
    }
  }],
  "from": 1,
  "last_page": 1,
  "next_page_url": null,
  "per_page": 10,
  "prev_page_url": null,
  "to": 1,
  "total": 1
}
我正在使用API资源

在我的资源中,我有:

   return parent::toArray($request);
有了这些,我得到了所有的信息,我的问题是我需要过滤一些数据 当我尝试过滤电子邮件的例子时

   return [
        'email'=> $this->email,
    ];
它给出了错误并说 “此集合实例上不存在属性[电子邮件]。”

我正在学习,我是一个新手,我为这个愚蠢的问题感到抱歉。 谢谢大家

在您的资源中:

$return = parent::toArray($request);

unset($return['email']);

return $return;
或者,您可以明确定义您想要返回的内容,而不是依赖于
toArray

return [
    'id' => $this->id,
    'message' => $this->message,
    // and so on...
];
这使您能够更好地控制最终的API响应,尤其是当您更改了数据库/模型,但需要API保持不变时。

首先创建一个特性

trait ResourceHelpers
{
    /**
     * Remove null values from Eloquent api resource
     * @param array $data
     * @return array
     */
    public function removeNullValues(array $data)
    {
        $filtered_data = [];
        foreach ($data as $key => $value) {
            $filtered_data[$key] = $this->when($value !== null, $value);
        }

        return $filtered_data;
    }
}
然后在你的资源中使用它

class UserResource extends JsonResource
{

    use ResourceHelpers;

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return $this->removeNullValues([
            "id" => $this->id,
            "first_name" => $this->first_name,
            "last_name" => $this->last_name,
            "phone" => $this->phone,
            "email" => $this->email,
            "balance" => $this->balance,
            'address' => $this->address,
            'city' => $this->city,
            'state' => $this->state,
            'zip_code' => $this->zip_code,
            'country' => CountryResource::make($this->whenLoaded('country')),
            "joined_at" => $this->created_at,
            "updated_at" => $this->updated_at,
        ]);
    }
}

我怀疑,你会遇到属性问题,因为
电子邮件
$this->sender
的属性,而不是
$this
在你显示的回复中。是的,电子邮件是sender的属性,但我怎么说返回sender->email呢?数组中的信息我只是想过滤:S,但没有得到所有的信息你要做的
'email'=>$this->sender->email
。当我试图明确说出我想要返回的内容时,它会出错!如果我将您所拥有的内容放在文章中,则此集合实例上不存在属性[id]。@自由职业者您应该阅读“资源集合”上的文档: