1

I know about PHP Array Objects but I have never seen laravel models working like this...Its kinda strange. Heres the actual code..

 public function getUserChats()
{
    $users = [];
    $chats1 = Chat::where('sender_id','=',auth()->user()->id)->get();
    $chats2 = Chat::where('reciever_id','=',auth()->user()->id)->get();
    foreach ($chats2->toArray() as $chat2) {
        $user = new UserResource(User::where('id','=',$chat2['sender_id'])->first());
        array_push($users, $user);
    }
    foreach ($chats1->toArray() as $chat1) {
        $user = new UserResource(User::where('id','=',$chat1['reciever_id'])->first());
        array_push($users, $user);
    }
    return $users;
}


//The above method works but this one doesn't works
 public function getUserChats()
{
    $users = [];
    $chats1 = Chat::where('sender_id','=',auth()->user()->id)->get();
    $chats2 = Chat::where('reciever_id','=',auth()->user()->id)->get();
    foreach ($chats2->toArray() as $chat2) {
        $user = new UserResource(User::where('id','=',$chat2->sender_id)->first());
        array_push($users, $user);
    }
    foreach ($chats1->toArray() as $chat1) {
        $user = new UserResource(User::where('id','=',$chat1->reciever_id)->first());
        array_push($users, $user);
    }
    return $users;
}

Notice inside the for each loop how I have to access the sender_id from $chat1 and $chat2 . I want to know whats actually going on .. Its more of a theoretical question...Thanks for the response

saketr64x
  • 56
  • 10
  • 2
    You're using `$chats2->toArray()`, so you get an array as result. In your second example, leave out `->toArray()` and it should work. – brombeer Feb 25 '18 at 07:52

2 Answers2

7

When you're using get() method, you're getting a collection of objects and not just one object, so you can iterate over the collection.

When you use $model['property'] syntax, it works because Model class implements the ArrayAccess interface. Laravel uses the offsetGet() method to return object property.

Also, you don't need to use ->toArray() method. You can iterate over a collection since Collection class implements IteratorAggregate.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Yes thats true...But You did not get my question....Notice inside the foreach loops how the $chat2 and $chat1 variables are acting as arrays not objects! – saketr64x Feb 25 '18 at 07:54
  • So if $chat1 is an object why it acts like an array? – saketr64x Feb 25 '18 at 08:00
  • @saketr64x if you're talking about the `$object['property']` syntax, please check the updated the answer. – Alexey Mezenin Feb 25 '18 at 08:05
  • Oh thanks....that helps so why the first example is working...its clear.....But why the second example doesn't works?....Is it because i am calling toArray()? – saketr64x Feb 25 '18 at 08:07
  • @saketr64x yes. You need to remove the `toArray()` method to avoid converting objects to arrays. – Alexey Mezenin Feb 25 '18 at 08:08
  • that will help...thnx...I really thought the toArray method converts collections to Array() ... but what it is doing is converting each item of a collection to array... – saketr64x Feb 25 '18 at 08:12
-1

In your first example, the interpreter recognizes ['sender_id'] as passing text. The brackets and single quote designate this.

In your second example, the interpreter is expecting sender_id to be a function of the Laravel Framework or PHP, as a result of the use of -> after the $variable.

Mike Stratton
  • 463
  • 1
  • 7
  • 20
  • As far as I know all the columns belonging to a table can be used as a property in the model...so in the second example it should just recognize it as a property and not as PHP or Laravel framework – saketr64x Feb 25 '18 at 08:05
  • Yes this is true but in your second example the interpreter does not recognize sender_id as being a reference to a table. – Mike Stratton Feb 25 '18 at 08:09