'variable value not getting displayed on laravel blade

I have following in my laravel blade file.

@slot('title') {{ $user->first_name }} {{ $user->last_name }} @endslot
        <?php if (App\Models\DepartmentUser::where('user_id',  $user->id)->exists()) {
            $jobTitleId=App\Models\DepartmentUser::where('user_id',  $user->id)->first()->job_title_id;
            if($jobTitleId){
                $jobTitle=App\JobTitle::where('id',$jobTitleId)->first()->title;
                echo '@slot'.$jobTitle.'@endslot';
            }
         }
         else{
                echo 'No job title set.';
            }
        ?>

Here, I'm trying to echo, user's job title if they are assigned to any department, and if they are not assigned to any then, No job title set text has to be shown..

But When I run this, it won't display me the job title.

I tried dd the $jobTitle it was printing the output...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source