orest's avatar
Level 13

override pagination data

when i query the activities table and eager load a relationship, i want the paginated data to contain only the relationship data and not the activity data.

array:12 [
  "current_page" => 1
  "data" => array:1 [
    0 => array:8 [
      "id" => 1
      "user_id" => "1"
      "type" => "created-thread"
      "subject_id" => "1"
      "subject_type" => "App\Thread"
      "created_at" => "2020-09-23T22:15:34.000000Z"
      "updated_at" => "2020-09-23T22:15:34.000000Z"
      "subject" => array:19 [
        "id" => 1

The subject is the eager loaded relationship and i need only the subject data

I know that i can grab the subject using pluck('subject')

collect($paginated)->pluck('subject');

but i can't figure out how to replaced the original paginated data with subjects

Essentially what i want to achieve is the following

$paginated['data'] = $paginated->pluck('subject')

And then finally return $paginated to the view.

0 likes
1 reply

Please or to participate in this conversation.