属性 [associated_occupation] 在此集合实例上不存在。 - Laravel

Property [associated_occupation] does not exist on this collection instance. - Laravel

我想将数据库值存储到变量中。由于某种原因,它没有按预期工作。下面是控制器中的代码:

 public function show(Word $word)
{
    //

    $curriculum = Curriculum::findOrFail($word->id);
   // dd($curriculum->id);
    session(['key' => $curriculum->id]);
    $knowledge = Knowledge::where('curriculum_id','=', $word->id)->get();
    $information = Information::where('curriculum_id','=', $word->id)->get();
    $practical = Practical::where('curriculum_id','=',$word->id)->get();
    $work = Work::where('curriculum_id','=',$word->id)->get();
    $entry = Entry::where('curriculum_id','=',$word->id)->get();
    $assessment = Assessment::where('curriculum_id','=',$word->id)->get();
    $parts = Part::where('curriculum_id','=',$word->id)->get();
    $occupurpose = Occupurpose::where('curriculum_id','=',$word->id)->get();
    $occutask = Occutask::where('curriculum_id','=',$word->id)->get();
    $taskdetail = Taskdetail::where('curriculum_id','=',$word->id)->get();
    $purposekm = Purposekm::where('curriculum_id','=',$word->id)->get();
    $guidedtopic = Guidetopic::where('curriculum_id','=',$word->id)->get();
    $purposepms = Purposepm::where('curriculum_id','=',$word->id)->get();
    $purposewem = Purposewem::where('curriculum_id','=',$word->id)->get();
    $guidedpmstopics = Guidedpmstopic::where('curriculum_id','=',$word->id)->get();
    $guidedwemstopics = Guidedwemstopic::where('curriculum_id','=',$word->id)->get();



    $wordTest = new \PhpOffice\PhpWord\PhpWord();
    $newSection = $wordTest->addSection();

    $SectionHeading = "SECTION 1:  CURRICULUM SUMMARY";
    $newSection->addText($SectionHeading);

    $topicHeading = "1. Occupational Information";
    $newSection->addText($topicHeading);
    $subTopic = "1.1 Associated Occupation";
    $newSection->addText($subTopic);
    $newSection->addText($knowledge->associated_occupation);
    $occupationTopic = "1.2 Occupation or Specialisation Addressed by this Curriculum";
    $newSection->addText($occupationTopic);
    $newSection->addText($knowledge->specialisation);
    $alternativeTopic = "1.3 Alternative Titles used by Industry";
    $newSection->addText($alternativeTopic);
    $newSection->addText($knowledge->alternative_title);

    $objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordTest,'Word2007');
    try {
        $objectWriter->save(storage_path('TestWordFile.docx'));
    } catch(Exception $e) {
    }

    return response()->download(storage_path('TestWordFile.docx'));
}

当我对课程 ID 和知识结果进行 dd 时,我得到了正确的反馈。现在我想存储列

associated_occupation into the variable called newSection but I get the error mentioned on the subject. I also tried $newSection->addText($knowledge->select('associated_occupation')); instead of $newSection->addText($knowledge->associated_occupation);

请协助。

您正在尝试访问集合的 属性:

$knowledge = Knowledge::where('curriculum_id','=', $word->id)->get();

以上将为您提供 curriculum_id$word->id

的结果集合

https://laravel.com/docs/5.6/queries#retrieving-results

The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object.

所以当你这样做时:

$newSection->addText($knowledge->associated_occupation);

$knowledge->associated_occupation 正在搜索结果集合中的 associated_occupation 而不是特定的 Knowledge

将您的查询更改为:

$knowledge = Knowledge::where('curriculum_id','=', $word->id)->first();

要获得第一个 Knowledge curriculum_id 作为 $word->id

https://laravel.com/docs/5.6/queries#retrieving-results

If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single StdClass object:

或者过滤集合并选择一个特定的集合