Question: I am trying to return the data of the database query then access it as an array in the blade file, but i keep getting this error:Undefined array key "cover\_img".

my controller

```
$id=request()->segment(count(request()->segments()));

$userdata=DB::table('users')->select('id','first_name','last_name','profile_img','cover_img')->where('id',$id)->get();

if($id!=auth()->id()) {
    return view('show-prof')->with(compact('userdata'));
} else {
    return view('profile');
}

```

my blade file

```
<div class="container">
    @if($userdata['cover_img']!=null)
        <img src="{{url('/images/user_cover_img/'.$userdata['cover_img'])}}" id="cover">
    @else
        <img src="/images/user_cover_img/cover_default.png" id="cover">
    @endif
</div>

```

I tested if the array is being returned by typing

```
<p>{{$userdata}}</p>

```

and the output was

[{"id":1,"first\_name":"mohamad","last\_name":"abedelhady","profile\_img":null,"cover\_img":null}]
I also tried dd($userdata)and the output was:

```
Illuminate\Support\Collection {#377 ▼
  #items: array:1 [▼
    0 => {#1255 ▼
      +"id": 1
      +"first_name": "mohamad"
      +"last_name": "abedelhady"
      +"profile_img": null
      +"cover_img": null
    }
  ]
}

```

Answer: 
Your query gives you an `Illuminate\Support\Collection` object (a [collection](https://laravel.com/docs/8.x/collections#introduction)) that contains anonymous objects (stdClass):

```php
// This 
$users = DB::table('users')->select('id','first_name','last_name','profile_img','cover_img')
            ->where('id',$id)->get();

```

I think you only want to get the user by id, pls do this.

```php
$user = DB::table('users')->select('id','first_name','last_name','profile_img','cover_img')
            ->where('id',$id)->first();

// Or
$user = DB::table('users')->select('id','first_name','last_name','profile_img','cover_img')
            ->whereKey($id)->first();

```

Because the `$user` is an anonymous object, you got to access the attributes this way:

```php
$user->cover_img;

```

Alternatively, if you have a `User` model, you can simply do this.

```php
$user = User::find($id);

// Then both are gonna work, but I recommend using the former.
$user->cover_img;
$user['cover_img'];

```
Let's looks at your Eloquent Quer here:

```
$userdata=DB::table('users')->select('id','first_name','last_name','profile_img','cover_img')
            ->where('id',$id)->get();

```

here get() method returns an array of user records. So instead of

```
$userdata['cover_img']!=null

```

You need to access cover\_img from the first element of the array. Like this:

```
$userdata[0]['cover_img']!=null

```

It seems you are only dealing with a single record and should use the `first()` method to only get the single object and then your code should work

```
$userdata=DB::table('users')->select('id','first_name','last_name','profile_img','cover_img')
            ->where('id',$id)->first();

```

**Remember**: The first() method will return only one record, while the get() method will return an array of records that you can loop over.