I'm learning Laravel and it uses OOPS concepts. Now I'm finding it hard to understand the real difference between array and objects. I actually know what an array and object is.
Array can hold more than one variable where as an object is an independent entity which has its own arguments and methods. We usually use foreach loop to loop through them.
In laravel, data is returned in the form of model instance as object. When the query response has multiple results, then data is returned in the form of an array which contains objects. I was trying to understand Collection Class used in laravel.
Codebright reference says
The Collection class itself, is merely a wrapper for an array of objects, but has a bunch of other interesting methods to help you pluck items out of the array.
Now coming back to my confusion. I was using different methods like all() and first() methods to fetch the result. But sometimes when i used arrow (->) to fetch the data using a foreach loop, from an object (contained in an array), it showed an error that says something like it is a non object. Then I used square brackets and the data was displayed.
I know we use [] to fetch data from arrays and we use -> to fetch data from objects. But I'm still confused about Laravel. Can someone clearly state the difference between them in reference to Collection class used in Laravel?
Edit:: The confusion began while using this code:
foreach($self_conversations as $self_conversations_fetch){
//fetching each conversation id
$conversation_id = Conversation::find($self_conversations_fetch->conversation_id);
$user_id = array();
//fetching each conversation member's id
foreach($conversation_id->conversationsMember as $conversationmembers)
$user_id[] = $conversationmembers->user_id;
$self_id = User::where('email', Session::get('email'))->first()->id;
$self_id_array = array($self_id);
$friend_id_array = array_diff($user_id, $self_id_array);
foreach($friend_id_array as $friend_id) array_push($friend_ids, $friend_id);
$conversations_reply_obj = ConversationReply::where('conversation_id', $self_conversations_fetch->conversation_id)->orderBy('updated_at', 'desc')->first();
$conversations_reply[] = $conversations_reply_obj['reply'];
}
As you can see, i have used square brackets to fetch the data(in the last line).
$conversations_reply[] = $conversations_reply_obj['reply'];
i was expecting arrow to work here