0

the problem is, when I pluck the ids, a collection of ids returned I need to get only the values

$transferOfficeId = $searchResults->pluck('office_id','office_id');
$vehicleId = $searchResults->pluck('vehicle_id','vehicle_id');
$agencyId = $searchResults->pluck('forAgency_id','forAgency_id');
$userId = $searchResults->pluck('addedBy_user_id','addedBy_user_id');

To get the names of these values by there ids but here is not allowed because the ids are a collection not a values

$officeName = $bookingTransfersData->pluck('transferOffice.officeName','transferOffice.transfer_office_id')->get($transferOfficeId);
$vehicleName = $bookingTransfersData->pluck('vehicle.vehicleName','vehicle.vehicleName')->get($vehicleId);
$agencyName = $bookingTransfersData->pluck('agency.agencyName','agency.agencyName')->get($agencyId);
$userName = $bookingTransfersData->pluck('user.userName','user.userName')->get($userId);


return view('dashboard.index',compact('searchResults','fromDestinationName','toDestinationName','officeName', 'vehicleName','agencyName','userName'))
    ->with('i', (request()->input('page', 1) - 1) * 5);

I want to get the values of ids plucked, to get columns by these ids from tables of each id like transferOfficeId,vehicleId .....

please help.

Sohaib
  • 1,972
  • 3
  • 9
  • 19
  • 1
    Please, simplify your question. There's a lot of unrelated code with the issue (I think). By the way, when you do `$collection->pluck('id','id');` you are getting an associative array that has the ids as keys and also the ids as values: `['1' => '1', '2' => '2', ..]` – Kenny Horna Jan 22 '20 at 21:47
  • thank for advice @KennyHorna, I simplified the question, I did that on purpose, but if I remove one like `$collection->pluck('id');` then I get like this `['0' => '1', '1' => '2', ..]` but you told me that i will get an associative array this when i add to pluck like this: `$collection->pluck('id')->toArray();` – Sohaib Jan 22 '20 at 22:05
  • `$collection->pluck('id');` return like that `Illuminate\Support\Collection {#498 ▼ #items: array:2 [▼ 1 => 1 2 => 2 ] }` – Sohaib Jan 22 '20 at 22:09
  • My bad, not the array straight away but when the toArray() method is attached. If you leave it as `->pluck(...)` you still get a Collection (so you could keep making modifications to the element). – Kenny Horna Jan 22 '20 at 22:13
  • sorry for that, I am new to Laravel, do you know the answer of my question above?@KennyHorna – Sohaib Jan 22 '20 at 22:17
  • I think I'm getting the problem. My question is, why are you doing all this operations in memory? You could get the related names from the database (offices, vehicle, agency, users, ...) using where/whereIn conditions – Kenny Horna Jan 22 '20 at 22:21
  • I want to make user search by entering some inputs if the inputs like the data in tables, then I need to show to user these inputs with the other data in the tables, so I have transfers table, and I have a vehicle_id column in it, then I want to get its name from vehicle table on condition of vehicle_id is from search result not from the transfers table @KennyHorna – Sohaib Jan 22 '20 at 22:36
  • @KennyHorna see this please [link]https://stackoverflow.com/questions/59886057/how-to-retrieve-different-names-from-same-table-with-different-ids-on-join-larav – Sohaib Jan 23 '20 at 20:12

0 Answers0