Ethris
Ethris

Reputation: 146

Laravel: Model functions called inside view

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.

Upvotes: 1

Views: 227

Answers (1)

hassan
hassan

Reputation: 8288

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); }}

Upvotes: 2

Related Questions