andy7's avatar
Level 1

How to use an if statement when a function returns a value

Hello everyone thank you for your time, I'm relatively new and learning, and I'm currently stuck a little at this part, I want to make it possible for each user when they request a sick(medical) leave from the system and if the number difference is bigger than 2 it should require that user to upload a file

So I have finished all these parts the functions to get the number difference and file upload I just need to put an if statement or another statement to check if it's more than 2 how would that be possible?

This is the function that gets the difference of the numbers from 2 dates

public function numberOfDays($id)
     {

        $datesx =Leave::where('id', $id)
        ->first();


         $from =$datesx->from;
         $to =$datesx->to;

         $datetime1 = new DateTime($from);
         $datetime2 = new DateTime($to);
         $datetime2->modify('+1 day');

         $interval = $datetime1->diff($datetime2);
         $days = $interval->format('%a');
         //dd($days);

        $period = new DatePeriod($datetime1, new DateInterval('P1D'), $datetime2);

        foreach($period as $dt) {
            $curr = $dt->format('D');
            if ($curr == 'Sat' || $curr == 'Sun') {
                $days--;
            }
        }
         return $days;
     }

And this is how i upload files on the leaveController on the store function

$request->file('medicalDocument')->storeAs('docs', $request->medicalDocument->getClientOriginalName());
0 likes
7 replies
andy7's avatar
Level 1

I've created a rule and am trying to figure out which would be best to get the number based on the type

public function passes($attribute, $value)
    {
	$leave = Leave::all();
        $days= $leave->numberOfDays($id);
        $type =$leave->type;

        if($type == "Sick Leave"){
            if($days > 2){

            }
        }

    }

Would something like this be good? It works on the controller but I'm not sure how it would work here I added each use app

automica's avatar

@andy7

you could set up an array of allowances, with the type as the key and allowed number as value:

eg

    $allowances = [
        'Sick Leave' => 2,
        'Holiday' => 7
    ];
    
    if ($days > $allowances[$type]) {
        return false;
    }
    return true;

1 like
automica's avatar

@andy7 you should be able to do a query

$leave = Leave::where('id',$value);
$days = $leave->numberOfDays;
$type = $leave->type;

to get the appropriate $leave record.

1 like
andy7's avatar
Level 1

Alright, I've added the one for the Sick because I only want it for that now would I need to add this to the controller?


$request->validate([
            'medicalDocument'      =>  ['required'],
]);

And then add the request for files?

            $request->file('medicalDocument')->storeAs('docs', $request->medicalDocument->getClientOriginalName());


automica's avatar

@andy7 you need to include the new validation method in your controller.

use App\Rules\NewValidationRule

and you'd use it like:

$request->validate([
            'medicalDocument'      =>  [ new NewValidationRule],
]);

see https://www.larashout.com/custom-validation-rules-in-laravel

you'll need to modify you passes method as you want to use the $value of your $request->days field rather than the $value you'll get from passed in from your new validation rule.

1 like
automica's avatar

I would create a custom request

php artisan make:request LeaveRequest

and then in your store & update you can use LeaveRequest $request rather than Request $request.

best to put validation in a separate class as that makes your controllers neater.

1 like

Please or to participate in this conversation.