1

Am I able to create a boolean function that returns me for example true if person is a woman and false if person is a man?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{
   public $fillable = ['name', 'surname', 'code'];
   public function queque(){
         return $this->hasMany(Queque::class);
   }
   public function gender(){
        if([GET CODE FROM THIS SPECIFIC USER][8] % 2 == 0){ //in my country 9th number od code is used to differ between genders
          return false; //man
     } else{
          return true; //woman
     }
   }
}

I want it to be used in view. So when i use it like {{$patient->gender}} it returns 0 or 1. Is it even possible? I know about query scopes but they are useless in view.

Any help would be greatly appreciated.

4
  • why do you want to use it directly from your model instead of implementing it within your controller and then pass it to your view file? Commented Apr 8, 2017 at 9:29
  • I want to use it inside my view. Commented Apr 8, 2017 at 9:32
  • I want to somehow automate process of checking 9th number in "code" and checking if it's divisible by 2 Commented Apr 8, 2017 at 9:33
  • So i can do something like @if($patient->gender) man @endif Commented Apr 8, 2017 at 9:34

1 Answer 1

2

you may pass your patient code as a parameter :

public static function gender($patientCode) {

if($patientCode % 2 == 0)
// .....

then call it within your view file as follows by passing your code to your method:

{{ App\Patient::gender($patient->code); }}
Sign up to request clarification or add additional context in comments.

6 Comments

Let's say I pass to my view a patient with it 1: $patient = Patient::find(1); And then I how can i specify what gender is he? How can i fullfill the space "GET CODE FROM THIS SPECIFIC USER" inside my model?
do you mean that this GET CODE FROM THIS SPECIFIC USER is an user input ?
Function inside my model. How can i get a field from database that's equal to code. So i can actually check the gender?
so you are asking about fetching data from db ? not passing it to your view files?
i am passing user into view. ONE user. I want to know what gender is he. How can i accomplish this knowing that i can get his gender only by CODE column inside mysql. IF 9th number of CODE is divisible by 2 then he's a man. How can i possibly access his gender from view. By doing something like $patient->gender. I think it's connected to accessors but i dont't know how to do it properly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.