samedi 26 décembre 2020

Is it possible to apply condition inside the map function of Laravel?

I wanted to apply some condition in foodOrders. Is it possible to apply condition inside map function that acts as where $foodOrder->foodOrders->where('invoice_id',null)->get();

 public function getTableList(Request $request){
    $skip =$request->skip;
    $limit=$request->limit;
    $totaltable = Table::get()->count();

    $table = Table::skip($skip)->take($limit)->orderBy('id', 'DESC')->get();
    $table->map(function($foodOrder){
        $foodOrder->foodOrders;
    });
}

Below is the output that this query returns. But I only want the data with null invoice_id

{
  "success": true,
  "message": "Lists of Table.",
  "data": [
    {
      "id": 2,
      "table_number": "TN02",
            "food_orders": [
        {
          "id": 16,
          "food_items_id": 1,
          "table_id": 2,
          "invoice_id": null,
          "quantity": 2,
          "price": "2000.00"
         },
        {
          "id": 17,
          "food_items_id": 2,
          "table_id": 2,
          "invoice_id": null,
          "quantity": 3,
          "price": "150.00"
        }
      ]
    },
    {
      "id": 1,
      "table_number": "TN01",
      "created_at": "2020-10-25 10:44:31",
      "updated_at": "2020-10-25 10:44:31",
      "food_orders": [
        {
          "id": 14,
          "food_items_id": 1,
          "table_id": 1,
          "invoice_id": 39,
          "quantity": 1,
          "price": "2000.00"
        }
      ]
    }
  ]
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3ppGNYO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire