user3386779
user3386779

Reputation: 7205

get all rows in single query with multiple id

I have a asset_request table with fields id and request_id.
I want to select multiple rows with specific ids.

$ids = $request->ids // 5,6

I want to select only rows with ids of 5 and 6 in request table

$ids = $request->ids;
$asset_request = asset_request::whereIn('id',array($ids))->first(); //gets only 6th row.

I need to get all rows matching the given ids.

Upvotes: 2

Views: 3979

Answers (3)

Option
Option

Reputation: 2645

To clarify after a chat discussion with the Op:

The Op was passing back a string request, therefore, the Op needed to change the following:

$id = $request->id;

$ids = str_split(str_replace(',', '', $id));

$asset_request = asset_request::whereIn('id', $ids)->get();

Upvotes: 1

chanafdo
chanafdo

Reputation: 5134

First you are calling the first method which will return only the first row matched.
You need to call get method to get all rows matched.

Secondly if you are sending ids as a comma separated string you need to convert it to array using explode.

$ids = $request->ids;
$asset_requst = asset_request::whereIn('id', explode(",", $ids))->get();

Upvotes: 1

Mike Foxtech
Mike Foxtech

Reputation: 1651

DB::table('asset_request')
           ->whereIn('id', (array) $request->ids)
           ->get();

or

TableModel::whereIn('id', (array) $request->ids)->get();

Upvotes: 0

Related Questions