jeudi 23 juillet 2020

Trying to get property 'id' of non-object on click of the edit button

I'm new to Laravel and Stackoverflow. I have this page where I'm displaying all the candidates who have applied for a job.

job-applications.blade.php

<table class="table table-hover table-striped" id="dataTable" style="font-size: 13px !important">

    <thead>
        <th style="padding:5px 22px 10px 6px !important">Candidate</th>
        <th style="padding:5px 22px 10px 6px !important">Status</th>
        <th style="padding:5px 22px 10px 6px !important">Edit</th>
    </thead>

    <tbody>

        @foreach ($applications as $application)

        <tr>
            <td>
            <a class="text-center" href=""

                target="_blank" class="text-theme"></a>
            </td>

            <td>
                
            </td>

            <td>
                    <a href="route('employers.applicants.edit', $applicant->id)" class="mt-1 text-center btn-sm btn btn-outline-yellow"> <i class="fa fa-pencil"></i> Edit</a> 
            </td>
        </tr>
    </tbody>
 </table>

Whenever I'm clicking on the edit button it is showing the error that

Trying to get property 'id' of non-object

Here's my controller code for jobApplications

public function jobApplications($slug)
{
    if (!Auth::check()) {

        session()->flash('error', 'Sorry !! You are not an authenticated Employer !!');

        return back();
    }

    $user = Auth::user();

    $user_id = $user->id;

    $applicant = DB::table('job_activities')->where('user_id',$user_id)->get();

    $job = Job::where('slug', $slug)->first();

    $expreience_data = Experience::all();

    $query = JobActivity::query();

    $applications = $query->where('job_id', $job->id)->get();

    $experiences = [];

    $education = [];

    $application_data = [];

    foreach ($applications as $application) {

        // Filter

        if (isset(request()->exp)) {

            $exp_data = CandidateProfile::with('experience')->where('experience_id', request()->exp)->where('user_id', $application->user_id)->first();


            if ($exp_data) {

                $experiences[] = $exp_data;

                $education[] = UserQualification::where('user_id', $application->user_id)->first();


                $application_data[] = $application;

                $filter['exp'] = request()->exp;
            }

        }else{

            $experiences[] = CandidateProfile::with('experience')->where('user_id', $application->user_id)->first();

            $education[] = UserQualification::where('user_id', $application->user_id)->first();

            $application_data[] = $application;
        }
    } 

    $education = $education ? $education[0] : [];

    $experience = $experiences ? $experiences[0] : [];

    $applications = $application_data;

    return view('frontend.pages.employers.job-applications', compact('user', 'applicant', 'job', 'applications','experience', 'education', 'filter', 'expreience_data'));

}

It is giving error on the line:

$applications = $query->where('job_id', $job->id)->get();

As per the solutions I could find over internet it's because I'm trying to assign a object type id to an array applications. I tried to change the first() to get() on $jobs variable but then it's giving the error that

property [id] doesn't exist on this collection instance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire