Make a bank GET from an Laravel Array

Asked

Viewed 180 times

1

I have a select where I do a search from an informed id, No Postman do a POST by sending an array to as in the example below

[
{  
      "id":"1"
},
{  
      "id":"2"
}
]

In the repository I search for these id to get all the information about them.

how I would look in my repository at the time of doing the search and bring the information of the currently informed ids I do a simple search with a get();

Repository:

Public function listarId($id)
    {
    return $this->produto = DB::table('produtos')
    ->where('id', $id)
    ->get();  
     }

and send as a parameter in the Postman one id at a time

  {  
  "id":"2"
  }

in my controller it is as follows, I send request with the variable $id, and return to select that I did in Repository.

   public function lista(Request $request) 
    {      

        $id = $request->input('id');

        return $this->produtosRepository->listarId($id);
    }

and I want to be able to send an array with several ids

1 answer

0


You instead of using Where(); you could use wherein();

For example:

 return $this->produto = DB::table('produtos')
->where('id_admin', $array_id)
->get();

Where is this $array_id would be an array of id’s.

OBS.: wherein necessarily needs to receive an array one-dimensional for example [1,2,3]

With this he will make a query similar to :

Select * from produtos where id_admin in (1,2);

I hope I’ve helped.

  • In the sending controller I have a Request where I pass the id parameter to do this search, but it returns null, when I send it this way [ ː "id":"1" }, ? "id":"2" } ] has some way to get the information from this array to do the search

  • the id you pass is generated here $idP = $this->produtosRepository->lista($id);?

  • yes, in would have yes, however if I send as an array with more than two $id to do the database search, return null, but send only one {"id" : "5"} it normally recognizes

  • Yes I have to understand how you take these values, when using Wherein you need to pass the simple array for example: whereIn('id', [1,2,3]);

  • well I edited the question I think now gives to better understand

  • in case the id array you will use in Where comes from that lista()? if yes runs a dd(); for me to see how this information is coming out, if not where this id’s from the search

  • The id comes from this variable $idarray = $request->input('id'); which I pass as parameter here $this ->productsRepository->listarId($idarray ); when I send only one id, sure, but more than one ja returns null

  • @Jessm rotates a dd(); in this Return to understand better what is going on

  • dd() ta returning [ ] empty

  • if dd(); is returning null the problem is in the id’s, or better as you are getting them,

  • yes, when I command the Postman {"id":"1"} it returns the values correctly, but when I send [{"id":"1"},{"id":"2"}] it returns empty

  • [{"id":"1"},{"id":"2"}] that you are commanding within the Wherein ? If it is ta manano a two-dimensional vector you have to pass only the ids, you will need to define which is passing the id

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.