Home > Blockchain >  Get particular values multidimensional array Laravel
Get particular values multidimensional array Laravel

Time:11-24

I want to get the values of some fields form the #attributes field (like "title" and "location") so I can fill a calendar.

dd($events); Outputs:

Illuminate\Database\Eloquent\Collection {#948 ▼
  #items: array:3 [▼
    "26-01-2021" => Illuminate\Database\Eloquent\Collection {#972 ▶}
    "01-02-2021" => Illuminate\Database\Eloquent\Collection {#962 ▶}
    "03-11-2021" => Illuminate\Database\Eloquent\Collection {#965 ▼
      #items: array:1 [▼
        0 => App\Models\DaysEvent {#994 ▼
          #table: "days_events"
          #casts: array:11 [▶]
          #dates: array:2 [▶]
          #fillable: array:25 [▶]
          #dispatchesEvents: array:1 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
           incrementing: true
          #with: []
          #withCount: []
           preventsLazyLoading: false
          #perPage: 15
           exists: true
           wasRecentlyCreated: false
          #escapeWhenCastingToString: false
          #attributes: array:29 [▼
            "id" => 166
            "title" => "Individual Interview"
            "slug" => "individual-interview"
            "functionality_type" => "selection"
            "days_event_type_id" => 1
            "event_start" => "2021-11-03 09:00:00"
            "event_end" => "2021-11-03 19:00:00"
            "location" => "Online"
            "excerpt" => "Suit up and sit down with a recruiter to get in touch with the company on an individual level."
            "content" => "<div>Suit up and sit down with a recruiter to get in touch with the company on an individual level. The Individual Interview will last 30 or 50 minutes and is e ▶"
            "event_link" => "https://zoom.us/j/1234"
            "parent_event_id" => null
            "requires_motivation" => 1
            "requires_cv" => 1
            "requires_followup" => 1
            "requires_student_company_preference" => 0
            "company_id" => 34
            "force_in_calendar" => 0
            "spots" => 9
            "has_timeslots" => 1
            "has_algorithm_priority" => 1
            "timeslot_slots" => "["9:00-9:30","9:30-10:00","10:00-10:30","11:00-11:30","11:30-12:00","12:00-12:30","13:00-13:30","13:30-14:00","14:00-14:30"]"
            "timeslot_people" => 1
            "can_unsubscribe_late" => 1
            "company_can_see_cvs" => 1
            "sf_id" => null
            "deleted_at" => null
            "created_at" => "2021-01-04 10:33:38"
            "updated_at" => "2021-11-16 12:33:00"
          ]
          #original: array:29 [▶]
          #changes: []
          #classCastCache: []
          #dateFormat: null
          #appends: []
          #observables: []
          #relations: array:1 [▶]
          #touches: []
           timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
          #forceDeleting: false
        }
      ]
      #escapeWhenCastingToString: false
    }
  ]
  #escapeWhenCastingToString: false
}

I think I can get the values using the collect function form Laravel. (read this on this very similar question)

But when I try do do: $event_items = collect($events->items);

It gives the error: Property [items] does not exist on this collection instance.

What am I doing wrong here?

CodePudding user response:

you are not get direct use of item, you use $event_items = collect($events['03-11-2021']['0']->title);

CodePudding user response:

This result you have is basically a collection of collections of models. Collections implement array access so you can user $collection['index'] or you can use $collection->get('index'). If you want all the values in the collection you can call the $collection->all() method. I don't really understand what data you want to get from here or why you have a collection of collections in the first place but the $collection->map() or $collection->transform() method is usually used to extract specific data from a collection.

I also believe that $collection->items is a private property so if you want all the items it is the same as $collection->all()

You can check Collections documentation for how they work.

  • Related