jeudi 3 mars 2016

Laravel 5 - Understanding why conditional statement required

I have the following Model structure

User
+----+--------------+-----------------+------------+---------------------+---------------------+
| id | userName     |      ...
+----+--------------+-----------------+------------+---------------------+---------------------+

UserActivity
+----+--------------+-----------------+------------+---------------------+---------------------+
| id | activityName |   userId
+----+--------------+-----------------+------------+---------------------+---------------------+

UserActivityType
+----+--------------+--------------------+------------+---------------------+---------------------+
| id | label        |   userActivityId
+----+--------------+--------------------+------------+---------------------+---------------------+

A User can have One UserActivity.

A UserActivity can have one to many UserActivityType

Now I have a page which seperates the displaying of content by tabs. Therefore whenever this page is loaded, all of the tab content is also loaded. Due to this, I need to handle the situation whereby I check that what I am trying to load exists, otherwise I will get a Trying to get a property of a ... error.

There is something I am trying to understand. On one of the pages, I am doing something like this

@if (count($user->userActivity) !== 0)
    @if (count($($user->userActivity->userActivityType) !== 0)
        @foreach($user->userActivity->userActivityType as $label => $value)

        @endforeach
    @endif
@endif

Now the above works without problems. However, because I am trying to get information from userActivityType, do I really need to check $user->userActivity first? Instead of doing the above, I would think it would make more sense just doing

@if (count($($user->userActivity->userActivityType) !== 0)
    @foreach($user->userActivity->userActivityType as $label => $value)

    @endforeach
@endif

However, doing this then throws the error mentioned above. Why would it throw an error just for this? I am still checking if it exists, but it does not seem to pay attention in this situation?

Any explanation appreciated.

Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Y4n8Kp
via IFTTT

Aucun commentaire:

Enregistrer un commentaire