Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to define global variable in if with php?

I have code something like this:

  @php
  if ($shires){
        $shire = $shires->first();
  }
  @endphp

  @if ($shire)
        {{ $shire->name }}
  @endif

I see this error

Undefined variable: shire

0 likes
12 replies
MichalOravec's avatar

It could be in if statement directly

@if ($shires->isNotEmpty() && $shire = $shires->first())
    {{ $shire->name }}
@endif
1 like
Sinnbeck's avatar

Or even simpler :)

@if ($shires->isNotEmpty())
    {{ $shires->first()->name }}
@endif

//or

{{ $shires->first()->name ?? '' }}
1 like
shahr's avatar
Level 10

I get this error.

Call to a member function isNotEmpty() on null

Sinnbeck's avatar

Please show how you are getting $shires

MichalOravec's avatar

If your plan was to use ->first() then you have to have a collection.

1 like
shahr's avatar
Level 10

In table of phpmyadmin.

shires

In Controller

public function job(User $user)
{
    $jobGroups = null;
    $employmentCenters = null;
    $howCooperates = null;
    $seniorLevels = null;
    $lands = null;
    $shires = null;
    $parishes = null;
    $starts = null;
    $ends = null;
    $jobGroup_ids = json_decode($user->job_group_id);
    if (isset($jobGroup_ids) && !empty($jobGroup_ids)) {
        $jobGroups = JobGroup::whereIn('id', $jobGroup_ids)->get();
    }
    $employmentCenter_ids = json_decode($user->employment_center_id);
    if (isset($employmentCenter_ids) && !empty($employmentCenter_ids)) {
        $employmentCenters = EmploymentCenter::whereIn('id', $employmentCenter_ids)->get();
    }
    $howCooperate_ids = json_decode($user->how_cooperate_id);
    if (isset($howCooperate_ids) && !empty($howCooperate_ids)) {
        $howCooperates = HowCooperate::whereIn('id', $howCooperate_ids)->get();
    }
    $seniorLevel_ids = json_decode($user->senior_level_id);
    if (isset($seniorLevel_ids) && !empty($seniorLevel_ids)) {
        $seniorLevels = SeniorLevel::whereIn('id', $seniorLevel_ids)->get();
    }
    $land_ids = json_decode($user->land_id);
    if (isset($land_ids) && !empty($land_ids)) {
        $lands = Country::whereIn('id', $land_ids)->get();
    }
    $shire_ids = json_decode($user->shire_id);
    if (isset($shire_ids) && !empty($shire_ids)) {
        $shires = Province::whereIn('id', $shire_ids)->get();
    }
    $parish_ids = json_decode($user->parish_id);
    if (isset($parish_ids) && !empty($parish_ids)) {
        $parishes = City::whereIn('id', $parish_ids)->get();
    }
    $start_ids = json_decode($user->start);
    if (isset($start_ids) && !empty($start_ids)) {
        $starts = Month::whereIn('id', $start_ids)->get();
    }
    $end_ids = json_decode($user->end);
    if (isset($end_ids) && !empty($end_ids)) {
        $ends = Month::whereIn('id', $end_ids)->get();
    }
    return view('Admin.users.jobs', compact('user', 'jobGroups', 'employmentCenters', 'howCooperates', 'seniorLevels', 'lands', 'shires', 'parishes', 'starts', 'ends'));
}
Sinnbeck's avatar

A quick fix is to default to a collection

$shires = null; //replace this
$shires = collect(); //with this
1 like
MichalOravec's avatar
Level 75

Instead of

$shires = null;

you can do

$shires = collect([]);

Or just

@if ($shires && $shire = $shires->first())
    {{ $shire->name }}
@endif
1 like
Sinnbeck's avatar

You dont need to add [] inside collect. It defaults to an empty array

Please or to participate in this conversation.