retrograde
retrograde

Reputation: 2979

Laravel Fluent Query - property of non-object

I am working on displaying user active jobs and pending interviews on a dashboard. I can get active jobs to work, but when I try to display pending interviews for the jobs, I get the error: "trying to get error of non-object." The error is for the line: $jobInterviews->id

Here is my dashboard view:

@if (! $active)
            <p> You don't have any active projects. Click here to post one!  </p>

            @else
                @foreach($active as $job)
            <div class="media-body">
                     display $job name/description
                    </div>
                    @if ($jobInterviews->id == $active->id)
                    <table class="table table-striped">
                        display $jobInterviews details
                  </table>
                    @else
                    <p></p>
                        @endif
            </div> <!-- .media-body -->
                    @endforeach
            @endif

        </div> <!-- .media -->

I have run through a @foreach loop with $active and $jobInterviews separately and both work fine. But when I nest the @if ($jobInterviews->id == $active->id), I get the error "attempting to display property of non object" related to $jobInterviews->id

Here is my controller method:

public function getdashboard()
    {

    //reading the user information
    $arrPageData['user'] = Sentry::getUser();

    //reading the job interviews
    $arrPageData['jobInterviews'] = JobInterview::readCurrentInterviews($this->userID);
            //reading the active jobs
    $arrPageData['active'] = Job::activeJobs($this->userID);

    return View::make('clients.dashboard', $arrPageData);
    }

and finally, here are my query statements:

 public static function readCurrentInterviews($jobID){
    return DB::table('jobs')
                ->join('job_interviews', 'job_interviews.job_id', '=', 'jobs.id')
                ->join('contractors', 'contractors.user_id', '=', 'job_interviews.send_to')
                ->where('jobs.user_id', '=', $jobID)

                ->where('job_interviews.status', '=', 'awaiting response')
                ->orderBy('job_interviews.created_at', 'desc')
                ->select('jobs.id','jobs.title', 'contractors.user_id', 'contractors.contact_name', 'job_interviews.status', 'jobs.created_at')
                ->get();
}



public static function activeJobs($contractorId){
   return DB::table('jobs')
        ->where('user_id', '=', $contractorId)
        ->select('id','title', 'description', 'created_at')
        ->get();
}

If anyone knows why this non-object error is being thrown, I would really appreciate the help understanding it. Thank you in advance.

Upvotes: 1

Views: 271

Answers (1)

The Alpha
The Alpha

Reputation: 146249

Just change following:

@if ($jobInterviews->id == $active->id)

To this:

@if ($jobInterviews->id == $job->id)

Here $active is a an array of models.

Also make sure that $jobInterviews is not the array of models and if it's also an array of models then $jobInterviews->id will not work. probably it's:

foreach($jobInterviews as $jobInterview)
    ...
    foreach($active as $job)
    @if ($jobInterview->id == $job->id)
        ...
    @endforeach
@endforeach

In your view the $jobInterviews and $active both are an array of multiple models and using $jobInterviews->id or $active->id will not work, instead you have to select an individual item from them.

Upvotes: 1

Related Questions