Web Student
Web Student

Reputation: 735

Laravel - What is the effective or logical design pattern to handle if / else in a view

Currently i have a pretty big project, where i need to store different data for different type of users.

I need to store body details for models.

The problem what i am stuck with is that, female will have different body information than male, and agencies, photographers dont need body information, and the users are in groups

group 3 - Model
group 4 - Agency
group 5 - photographer

In my views i am currently doing this

if ($group == 3 && $gender == 2) 
{
    // load female body information (lot of details)
} 
elseif ($group == 3 && $gender == 1) 
{
   // load male body information (lot of details)
} 
else 
{
    // load just work explerience & company info
}

Is store the users information broken in 3 tables

users         . login information
users_details - users details, like first last name gender ect
users_body    - Body information for models both female / male

So why i am asking for a hint for a more effective way because i will need this type of information shown on 3 different pages, and i think it will get messy and will result lot of code duplications

If someone could give me a hint on any advice would be happy

thank you

Upvotes: 3

Views: 691

Answers (2)

Havelock
Havelock

Reputation: 6986

As it's been already said, you should handle this outside of the view.
The words store data and the representation in DB tables should be a hint to use models for that. You might have a parent (maybe abstract) class Model or Body which you might extend in sub classes Male and Female.
For handling different user roles, usually ACL (Access Control Level) is the right strategy.
Also, all this should be logically handled by the controller, from which you can pass the desired and filtered information to the view.

Upvotes: 3

daylerees
daylerees

Reputation: 1006

Handle this logic outside the view and have the result of the logic set a variable for the view with the appropriate result. As for avoiding the 'if-tree' the strategy pattern may come in useful depending on the situation.

Upvotes: 5

Related Questions