I have a method returning all job post data. This is what I have tried:
public function show($id)
{
$applicantData = Applicant::whereId($id)->first();
$jobData = Job::all();
//dd($jobData);
//dd($jobData->job_title);
return view('applicant.confirmation',compact("applicantData","jobData"));
}
dd($jobData);
returns :
dd($jobData->job_title);
it returns an error:
ErrorException in ApplicantController.php line 150: Undefined property: Illuminate\\Database\\Eloquent\\Collection::$job_title
How can I get the $jobData->job_title
value?
You should iterate collections using the each
method in Laravel:
$jobData->each(function ($item, $key) {
dd($item->job_title);
});
You have to loop through $jobData
since it will return all rows. Something like:
$jobData = Job::all();
foreach ($jobData as $job) {
echo $job->job_title;
}
They are object in arrays so you can directly pickout the value. If you need first item then $jobData[0]->job_title
or $jobData->first()->job_title
or you can loop foreach($jobData as $data){$data->job_title}
$jobData
is a collection so you cannot directly access job_title
like $jobData->job_title
. I figure you want to access them in your view file. hence do following
@foreach($jobData as $job)
{{ $job->job_title }}
@endforeach
public function show($id)
{
$applicantData = Applicant::whereId($id)->first();
$jobData = Job::find($applicantData->job_id); //this will give you the job
dd($jobData);
dd($jobData->job_title);
return view('applicant.confirmation',compact("applicantData","jobData"));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.