Skip to content
Advertisement

Undefined index , Unable to fetch columns- Laravel

 $notification=Notifications::where('status','false')->get();
 $data=$notification->all();
 //dd($data);
 dd($data['notificationTypeName']);

In the above code, Notifications is the model Name. Getting the error Undefined index: notificationTypeName when trying to access column notificationTypeName of the array $data.

dd($data) gives an ouput as below:

array:4 [
  0 => AppModelsNotifications {#323
    #table: "notifications"
    #fillable: array:6 [
      0 => "notificationTypesId"
      1 => "notificationTypeName"
      2 => "userId"
      3 => "email"
      4 => "status"
      5 => "recipientName"
    ]
    #connection: "pgsql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    +preventsLazyLoading: false
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:9 [
      "id" => 5
      "notificationTypesId" => 3
      "notificationTypeName" => "Cart Expired"
      "userId" => 1
      "email" => "me.g@k.com"
      "recipientName" => "John"
      "status" => false
      "created_at" => null
      "updated_at" => null
    ]
    #original: array:9 [
      "id" => 5
      "notificationTypesId" => 3
      "notificationTypeName" => "Cart Expired"
      "userId" => 1
      "email" => "me.g@k.com"
      "recipientName" => "John"
      "status" => false
      "created_at" => null
      "updated_at" => null
    ]
    #changes: []
    #casts: []
    #classCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #guarded: array:1 [
      0 => "*"
    ]
    #enableLoggingModelsEvents: true
    #oldAttributes: []
  }

Advertisement

Answer

The all method returns the underlying array represented by the collection:

see this : https://laravel.com/docs/8.x/collections#method-all

so try with this:

 dd($data[0]['notificationTypeName']) // if u r not going to use foreach

with foreach

foreach( $notifications as $notification)
{
  dd($notification->notificationTypeName);
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement