Lỗi Undefined variable: jobs trong laravel

Cho em hỏi:
khi nhấn nút add new: đường đẫn là jobs\create
bị lỗi underfile biến jobs trong hàm index:

public function index(Request $request, $job_type =null)
    {
        
        if($job_type ==null){
            $jobs = $this->jobRepository->all();
        }else if($job_type == 'all_jobs'){
            Flash::success('all_jobs');
            $jobs = Job::where('status', 'active')->get();
        }
        else if($job_type == 'jobs_i_created'){
            Flash::success('jobs_i_created');
            $jobs = Job::where('user_id', Auth::user()->id)->get();
        }
        if($job_type == 'jobs_i_applied_for'){
            Flash::success('jobs_i_applied_for');
            $jobs = Invitation::where('user_id', Auth::user()->id)->get();
        }else if($job_type =='admin_jobs' && Auth::check() && Auth::user()->role_id <3 ){
            Flash::success('admin_jobs');
            $jobs = $this->jobRepository->all();
        }  
// nó báo lỗi chỗ này
        return view('jobs.index')
            ->with('jobs', $jobs);
    }

Cho hỏi, khi nhấn addnew thì thằng này nó sẽ chạy cái hàm create này:

public function create()
    {
        $countries = Country::all();
        $organisations = Organisation::where('user_id', Auth::user()->id)->get();
   
        return view('jobs.create')
        ->with('job', $job)
        ->with('organisations', $organisations)
        ->with('countries', $countries);
  
    }

Nó có liên quan gì cái hàm index kia mà lại báo lỗi thằng jobs trong hàm index. Em xin cách sửa luôn ạ. Cảm ơn nhiều <3

Theo mình thấy, có lẽ biến $jobs chưa được khai báo. Nên khai báo nó trước if - else.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?