Laravel 6 Undefined variable: jobs

539 views Asked by At

I think there is no error but Laravel shows the following error:

Undefined variable: jobs

public function jobCreate(Request $request){

    $data['jobs'] = DB::table('jobs')->get();
    $job_category = JobCategory::all();
    // $job_locations = JobLocation::all();
    if(Auth::user()->user_type == 'admin'){
        return view('backend.job_circuler.create',compact('job_category',$data));
    }
    elseif(Auth::user()->user_type == 'customer'){
        return view('frontend.user.job_circuler.create', compact('job','job_category'));
    }
    else {
        abort(404);
    }

}

I import all that things properly but not working.

                <div class="form-group row" id="location">
                    <label class="col-md-3 col-from-label">
                        {{translate('Location')}}
                        <span class="text-danger">*</span>
                    </label>
                    <div class="col-md-9">
                        <select class="form-control aiz-selectpicker" name="location_id" id="location_id" data-live-search="true" >
                            <option >select location</option>
                            @foreach($jobs as $job)
                            <option value="{{ $job->location_id }}">
                                {{ $job->joblocation->location  }}
                            </option>
                            @endforeach
                        </select>
                        @error('location')
                 <div class="alert alert-danger">{{ $message }}</div>
                            @enderror
                    </div>
                </div>
             

still showing this error

1

There are 1 answers

0
shaedrich On

According to the error message, the view, you've posted is backend.job_circuler.create. Am I right?

The error says, the variable $jobs is missing and it is right: You pass $job_category as string, but $data as an array. You indeed can pass arrays to compact() but only if they contain variable names instead of actual variables. Why do you even wrap the $jobs inside $data?

$jobs = DB::table('jobs')->get();
$job_category = JobCategory::all();
if(Auth::user()->user_type == 'admin'){
    return view('backend.job_circuler.create', compact('job_category', 'jobs'));
}

Of course, it also works the other way round:

$data['jobs'] = DB::table('jobs')->get();
$data['job_category'] = JobCategory::all();
if(Auth::user()->user_type == 'admin'){
    return view('backend.job_circuler.create', $data);
}

Or, if you—for some reason—don't want to change the code above the if, you can still do this:

$data['jobs'] = DB::table('jobs')->get();
$job_category = JobCategory::all();
if(Auth::user()->user_type == 'admin'){
    return view('backend.job_circuler.create', [ ...$data, 'job_category' => $job_category ]);
}

or this:

$data['jobs'] = DB::table('jobs')->get();
$job_category = JobCategory::all();
if(Auth::user()->user_type == 'admin'){
    return view('backend.job_circuler.create', [ ...$data, ...compact('job_category') ]);
}